帮忙写一个桌面日历备忘录代码
时间: 2023-12-28 16:18:55 浏览: 145
好的,以下是一个简单的桌面日历备忘录的 Python 代码,你可以在本地运行:
```python
import tkinter as tk
import calendar
from tkinter import messagebox
class CalendarGUI:
def __init__(self, root):
self.root = root
self.root.title("日历备忘录")
self.year = tk.StringVar()
self.month = tk.StringVar()
self.date = tk.StringVar()
self.event = tk.StringVar()
self.create_widgets()
def create_widgets(self):
self.label_year = tk.Label(self.root, text="请输入年份:")
self.entry_year = tk.Entry(self.root, textvariable=self.year)
self.label_month = tk.Label(self.root, text="请输入月份:")
self.entry_month = tk.Entry(self.root, textvariable=self.month)
self.label_date = tk.Label(self.root, text="请输入日期:")
self.entry_date = tk.Entry(self.root, textvariable=self.date)
self.label_event = tk.Label(self.root, text="请输入备忘事件:")
self.entry_event = tk.Entry(self.root, textvariable=self.event)
self.btn_show = tk.Button(self.root, text="显示日历", command=self.show_calendar)
self.btn_add = tk.Button(self.root, text="添加备忘", command=self.add_event)
self.btn_quit = tk.Button(self.root, text="退出", command=self.root.quit)
self.label_year.grid(row=0, column=0, padx=5, pady=5)
self.entry_year.grid(row=0, column=1, padx=5, pady=5)
self.label_month.grid(row=1, column=0, padx=5, pady=5)
self.entry_month.grid(row=1, column=1, padx=5, pady=5)
self.label_date.grid(row=2, column=0, padx=5, pady=5)
self.entry_date.grid(row=2, column=1, padx=5, pady=5)
self.label_event.grid(row=3, column=0, padx=5, pady=5)
self.entry_event.grid(row=3, column=1, padx=5, pady=5)
self.btn_show.grid(row=4, column=0, padx=5, pady=5)
self.btn_add.grid(row=4, column=1, padx=5, pady=5)
self.btn_quit.grid(row=5, column=0, columnspan=2, padx=5, pady=5)
def show_calendar(self):
year = int(self.year.get())
month = int(self.month.get())
cal = calendar.monthcalendar(year, month)
self.calendar_window = tk.Toplevel(self.root)
self.calendar_window.title("日历")
for i in range(len(cal)):
for j in range(len(cal[i])):
if cal[i][j] == 0:
continue
tk.Label(self.calendar_window, text=cal[i][j]).grid(row=i, column=j, padx=5, pady=5)
def add_event(self):
year = self.year.get()
month = self.month.get()
date = self.date.get()
event = self.event.get()
if not year or not month or not date or not event:
messagebox.showerror("错误", "请输入完整信息")
return
with open("events.txt", "a") as f:
f.write(f"{year}-{month}-{date} {event}\n")
messagebox.showinfo("提示", "备忘添加成功")
root = tk.Tk()
app = CalendarGUI(root)
root.mainloop()
```
这个代码使用了 Tkinter 库来创建 GUI 界面和显示日历,使用了 Python 自带的 `calendar` 模块来生成日历。同时还可以添加备忘事件,将备忘事件保存到本地的 `events.txt` 文件中。你可以根据实际需求对代码进行修改和完善。
阅读全文