python tkinter 显示日历
时间: 2023-11-09 07:00:16 浏览: 80
python日历
可以使用 Python 内置的 calendar 模块和 tkinter 模块来显示日历。以下是一个简单的示例代码:
```python
import calendar
import tkinter as tk
class CalendarGUI:
def __init__(self, master):
self.master = master
self.master.title("Calendar")
self.year = tk.StringVar()
self.month = tk.StringVar()
self.create_widgets()
def create_widgets(self):
tk.Label(self.master, text="Year:").grid(row=0, column=0)
tk.Entry(self.master, textvariable=self.year).grid(row=0, column=1)
tk.Label(self.master, text="Month:").grid(row=1, column=0)
tk.Entry(self.master, textvariable=self.month).grid(row=1, column=1)
tk.Button(self.master, text="Show", command=self.show_calendar).grid(row=2, column=0, columnspan=2)
def show_calendar(self):
year = int(self.year.get())
month = int(self.month.get())
cal = calendar.monthcalendar(year, month)
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
# Create header
for i, day in enumerate(days):
tk.Label(self.master, text=day).grid(row=3, column=i)
# Create calendar
for i, week in enumerate(cal):
for j, day in enumerate(week):
if day != 0:
tk.Label(self.master, text=str(day)).grid(row=i+4, column=j)
root = tk.Tk()
app = CalendarGUI(root)
root.mainloop()
```
阅读全文