📅  最后修改于: 2023-12-03 14:46:26.024000             🧑  作者: Mango
如果需要为 Python 项目创建基于 GUI 的日历,Tkinter 库是不错的选择。Tkinter 是 Python 语言的标准 GUI 库,其内置日历组件可以很好地完成日历显示的工作。
下面是使用 Tkinter 创建日历的示例代码。代码中使用了 Python 内置的 datetime 模块来处理日期相关的逻辑。
import calendar
import tkinter as tk
from tkinter import ttk
class Calendar:
def __init__(self, parent, values):
self.values = values
self.parent = parent
self.cal = calendar.Calendar(firstweekday=calendar.SUNDAY)
self.year = datetime.date.today().year
self.month = datetime.date.today().month
self.day = datetime.date.today().day
self.current = self.cal.monthdayscalendar(self.year, self.month)
self.selected = None
self.create_calendar()
def clear(self):
for child in self.parent.winfo_children():
child.destroy()
def go_prev(self):
if self.month == 1:
self.month = 12
self.year -= 1
else:
self.month -= 1
self.selected = None
self.current = self.cal.monthdayscalendar(self.year, self.month)
self.clear()
self.create_calendar()
def go_next(self):
if self.month == 12:
self.month = 1
self.year += 1
else:
self.month += 1
self.selected = None
self.current = self.cal.monthdayscalendar(self.year, self.month)
self.clear()
self.create_calendar()
def selection(self, day, i):
self.selected = day
for c in self.values:
c.set('')
self.values[i].set(self.selected)
def create_calendar(self):
months = [
'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
]
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
font_header = ('Verdana', 14)
font_weekdays = ('Verdana', 10, 'bold')
curr_month = months[self.month-1]
head = tk.Label(self.parent, height=1, text=f"{curr_month} {self.year}", font=font_header)
prev_button = ttk.Button(self.parent, text='Prev', command=self.go_prev)
next_button = ttk.Button(self.parent, text='Next', command=self.go_next)
cal_frame = tk.Frame(self.parent, bd=2, relief='flat')
head.grid(row=0, column=0, columnspan=7)
prev_button.grid(row=0, column=0, padx=10)
next_button.grid(row=0, column=6, padx=10)
weekdays = [tk.Label(cal_frame, text=i, font=font_weekdays) for i in days]
for i, day in enumerate(days):
weekdays[i].grid(row=0, column=i)
for week in self.current:
week_frame = tk.Frame(cal_frame)
week_frame.grid(row=self.current.index(week) + 1, column=0)
for i, day in enumerate(week):
if day == 0:
day_button = ttk.Button(week_frame, text='')
day_button.grid(row=0, column=i)
else:
day_button = ttk.Button(week_frame, text=str(day), command=lambda day=day: self.selection(day, i))
day_button.grid(row=0, column=i)
if self.selected == day:
day_button.config(style='selected.TButton')
cal_frame.grid(row=1, column=0, pady=10)
以下是如何使用上面的示例代码创建一个简单的 GUI 日历:
import datetime
import tkinter as tk
from tkinter import StringVar
from tkinter import ttk
from calendar_app import Calendar
# 创建应用
app = tk.Tk()
app.title('Python 日历')
# 创建日历
values = [StringVar() for _ in range(42)]
cal = Calendar(app, values)
cal.create_calendar()
# 运行应用
app.mainloop()
上面的代码只是一个简单的 Tkinter 日历示例,可以根据需要进行自定义。最终的 UI 效果如下:
可以看到,Tkinter 不仅易于使用,还提供了灵活的定制选项,使其成为 Python 项目中基于 GUI 的日历的理想选择。