Reputation: 11
I am currently writing a python calendar program for a college assignment. In my program, a user selects the day they want to add an event/reminder/note to. I am just wondering what would be the best way to store these note strings, as well as, recalling them for the reminder/alarm.
Note: This program is still a WIP, and I am still learning.
Thank you, and any help is greatly appreciated
After some consideration, and some research, I have seen that some people have suggested lists/sets/dictionaries. My main concern is that, would any of these be able to handle multiple users inputs and/or multiple inputs by the same user?
from tkinter import *
import tkinter as tk
from tkcalendar import Calendar
def add_note():
selected_date = cal.get_date()
note = note_entry.get()
# Here you would add code to set an alarm or reminder for the selected date.
# This could involve using a reminder system, sending notifications, or using a scheduler.
# Below is a placeholder message indicating that the alarm has been set.
alarm_message = f"Alarm set for {selected_date}." # Placeholder message
# Display the note and alarm message in the label
date.config(text="Note added: " + note + selected_date + "\n" + alarm_message)
# Function to set frame size relative to window size
def update_frame_size(event):
# Calculate actual width and height based on window size
frame_width = int(root.winfo_width() * frame_width_fraction)
frame_height = int(root.winfo_height() * frame_height_fraction)
# Update frame widget dimensions
frame.config(width=frame_width, height=frame_height)
# Create Object
root = tk.Tk()
root.title('TaskMagnet')
# Set geometry
root.geometry("720x600")
# Define relative size for the frame containing the calendar
frame_width_fraction = 0.8
frame_height_fraction = 0.6
# Create frame to hold the Calendar widget
frame = tk.Frame(root)
frame.pack(pady=20)
# Add Calendar with relative size
cal = Calendar(frame, selectmode='day',
year=2020, month=5,
day=22)
cal.pack(fill='both', expand=True)
# Bind window resize event to update_calendar_size function
root.bind('<Configure>', update_frame_size)
# Label and Entry for the note
note_label = tk.Label(root, text="Enter Note:")
note_label.pack()
note_entry = tk.Entry(root)
note_entry.pack()
# Add Button and Label
tk.Button(root, text="Add Note and Alarm",
command=add_note).pack(pady=20)
date = Label(root, text = "")
date.pack(pady = 20)
# Execute Tkinter
root.mainloop()
Upvotes: 1
Views: 119
Reputation: 386382
I would argue that using sqlite would be the best solution, for a couple of reasons. First, it's perfect for storing data such as this, making it really easy to support multiple users, multiple fields per record, and so on. Also, because you are just now learning about programming, sqlite is a really powerful tool that is good to learn.
Sqlite is unlike most sql databases - the data is all stored in a single portable file, and does not require a server. It is remarkably robust, one of the finest examples of software engineering out there. Easy to use, reliable, scaleable, and so on.
You can literally open, define, insert, and search. your data in half a dozen lines of very basic code.
For a nice introduction to how to use sqlite, see sqlite3 — DB-API 2.0 interface for SQLite databases in the official python documentation.
Upvotes: 2
Reputation: 35
You can create a parallel array or so-called records to help you store the data. I would recommend to either have a SQL database which stores the users (since I perceive that you want the idea to have users and so forth) or write to an external file (txt or csv works) locally.
records would look like this
import dataclass from dataclasses
@dataclass
class [whatever you name it]:
variableName: type = [blank]
example1: str = ""
example2: int = 0
Then you can assign that record to an array (list so to speak) which would look like this:
arrayName = [recordName(i) for i in range(however much record you want to store)]
To access the records (for assignment and so forth) you can do something like this (using the example above):
exampleArray[index].recordName = "Hello World"
exampleArray[0].example1 = "Hello World"
Since the records are declared in an array, whenever you want to access it, you should use the array, then the index will be what record you are accessing, and the dot notation will be the "field" you want to edit.
Hopefully this helps.
Upvotes: 2
Reputation: 9
The problem with dictionaries is that your data will be gone the moment you stop the script. Certainly the best way to store the data would be an SQL database where you assign a unique ID for each user. But you can get with simply writing your data into a file. It can be a JSON format so that you also can use user identifiers as unique keys. Then you can use nested dictionaries to assign whatever fields you like for the user.
import json
data = {}
...
note = note_entry.get()
note_data = {datetime: note_content}
data[user_id] = note_data
with open('data.json', 'a') as f:
json.dump(data, f)
Upvotes: 1