图形界面万年历python
时间: 2023-06-21 15:14:06 浏览: 111
图形化界面的万年历代码
以下是一个简单的 Python 代码,用于创建一个基本的图形界面万年历:
```python
import calendar
import tkinter as tk
class Calendar:
def __init__(self, parent):
self.parent = parent
self.cal = calendar.TextCalendar(calendar.SUNDAY)
self.year = 2021
self.month = 1
self.wid = []
self.day_selected = 1
self.month_selected = self.month
self.year_selected = self.year
self.day_name = ""
self.setup()
self.rebuild_calendar()
def setup(self):
# 标题
header = tk.Frame(self.parent)
header.pack(fill="x", pady=10)
tk.Label(header, text='My Calendar', font=('Arial',20)).pack()
# 月份和年份选择
self.year_var = tk.StringVar()
self.month_var = tk.StringVar()
self.year_var.set(self.year)
self.month_var.set(self.month)
month_year_frame = tk.Frame(self.parent)
month_year_frame.pack()
tk.Label(month_year_frame, text='Year:', font=('Arial', 16)).grid(row=0, column=0)
tk.Label(month_year_frame, text='Month:', font=('Arial', 16)).grid(row=0, column=2)
year_spin = tk.Spinbox(month_year_frame, from_=1900, to=2099, width=5, font=('Arial', 16), textvariable=self.year_var, command=self.rebuild_calendar).grid(row=0, column=1)
month_spin = tk.Spinbox(month_year_frame, values=('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'), width=5, font=('Arial', 16), textvariable=self.month_var, command=self.rebuild_calendar).grid(row=0, column=3)
# 日历主体
self.main = tk.Frame(self.parent)
self.main.pack()
# 星期名称
days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
for i, day in enumerate(days):
tk.Label(self.main, text=day, font=('Arial', 16)).grid(row=1, column=i)
# 日历日期
for week in range(6):
for day in range(7):
self.wid.append(tk.Label(self.main, font=('Arial', 16), width=4))
self.wid[-1].grid(row=week+2, column=day)
self.wid[-1].bind('<Button-1>', self.day_click)
# 今天按钮
today_button = tk.Button(self.parent, text='Today', width=15, command=self.today)
today_button.pack(pady=10)
def rebuild_calendar(self):
self.year_selected = int(self.year_var.get())
self.month_selected = int(self.month_var.get())
calendar_month = self.cal.monthdayscalendar(self.year_selected, self.month_selected)
for i in range(len(calendar_month)):
week = calendar_month[i]
week_index = i+2
for j in range(len(week)):
day = week[j]
wid_index = i*7+j
if day == 0:
self.wid[wid_index].config(text='')
else:
self.wid[wid_index].config(text=day)
if day == self.day_selected:
self.wid[wid_index].config(bg='blue')
else:
self.wid[wid_index].config(bg='#FFFFFF')
month_name = calendar.month_name[self.month_selected]
self.year_name = self.year_selected
self.day_name = ""
header_text = month_name + " " + str(self.year_name)
self.parent.title(header_text)
def day_click(self, event):
widget = event.widget
self.day_selected = int(widget['text'])
self.rebuild_calendar()
def today(self):
self.year_selected = 2021
self.month_selected = 1
self.day_selected = 1
self.year_var.set(self.year_selected)
self.month_var.set(self.month_selected)
self.rebuild_calendar()
if __name__ == '__main__':
root = tk.Tk()
root.geometry('600x500')
app = Calendar(root)
root.mainloop()
```
这个代码使用了 `tkinter` 模块来创建图形界面,并使用 `calendar` 模块来生成万年历。在界面上,用户可以选择不同的月份和年份,可以点击日期,标记选中的日期,并且可以通过一个“今天”按钮返回到当前日期。
阅读全文