python tkinter 日历
时间: 2023-11-01 14:08:48 浏览: 152
Python tkinter 下拉日历控件代码
5星 · 资源好评率100%
你可以使用Python的Tkinter库创建一个简单的日历。下面是一个示例代码可以帮助你开始:
```python
import calendar
import tkinter as tk
def show_calendar():
# 获取当前选定的年份和月份
year = int(year_entry.get())
month = int(month_entry.get())
# 创建日历
cal = calendar.monthcalendar(year, month)
# 清空之前的日历
for widget in calendar_frame.winfo_children():
widget.destroy()
# 在界面上显示日历
for week in cal:
week_labels = []
for day in week:
week_labels.append(tk.Label(calendar_frame, text=day))
for label in week_labels:
label.pack(side=tk.LEFT)
# 创建主窗口
root = tk.Tk()
root.title("Calendar")
# 创建输入框和按钮
year_label = tk.Label(root, text="Year:")
year_label.pack()
year_entry = tk.Entry(root)
year_entry.pack()
month_label = tk.Label(root, text="Month:")
month_label.pack()
month_entry = tk.Entry(root)
month_entry.pack()
show_button = tk.Button(root, text="Show Calendar", command=show_calendar)
show_button.pack()
# 创建日历框架
calendar_frame = tk.Frame(root)
calendar_frame.pack()
# 运行主循环
root.mainloop()
```
阅读全文