我目前正在为大学作业编写一个Python日历程序。 在我的程序中,用户选择他们想要添加事件/提醒/注释的日期。 我只是想知道存储这些音符字符串以及调用它们以进行提醒/警报的最佳方式是什么。
注意:这个程序仍然是一个 WIP,我还在学习中。
谢谢您,非常感谢任何帮助
经过一番考虑和研究,我发现有些人建议使用列表/集合/字典。我主要关心的是,其中任何一个都能够处理多个用户输入和/或同一用户的多个输入吗?
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()
字典的问题是,当你停止脚本时,你的数据就会消失。 当然,存储数据的最佳方式是 SQL 数据库,您可以在其中为每个用户分配唯一的 ID。 但您只需将数据写入文件即可。它可以是 JSON 格式,以便您还可以使用用户标识符作为唯一键。然后您可以使用嵌套字典为用户分配您喜欢的任何字段。
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)