图形界面万年历python
时间: 2023-10-12 20:15:34 浏览: 94
好的,下面是一个基于 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 = tk.StringVar()
self.month = tk.StringVar()
self.year.set(str(self.cal.formatyear(2021)))
self.month.set(str(self.cal.formatmonth(2021, 1)))
self.setup()
def setup(self):
frame = tk.Frame(self.parent)
frame.pack(side=tk.TOP, pady=10)
tk.Label(frame, text='Year:', font=('Arial', 14)).pack(side=tk.LEFT)
tk.Entry(frame, textvariable=self.year, font=('Arial', 14), width=10).pack(side=tk.LEFT, padx=5)
tk.Label(frame, text='Month:', font=('Arial', 14)).pack(side=tk.LEFT)
tk.Entry(frame, textvariable=self.month, font=('Arial', 14), width=10).pack(side=tk.LEFT, padx=5)
tk.Button(frame, text='Show', font=('Arial', 14), command=self.show_calendar).pack(side=tk.LEFT, padx=5)
self.text = tk.Text(self.parent, font=('Arial', 12), height=15, width=25)
self.text.pack(side=tk.TOP, pady=10)
def show_calendar(self):
year = int(self.year.get())
month = int(self.month.get())
cal_str = self.cal.formatmonth(year, month)
self.text.delete('1.0', tk.END)
self.text.insert(tk.END, cal_str)
if __name__ == '__main__':
root = tk.Tk()
root.title('Calendar')
app = Calendar(root)
root.mainloop()
```
该程序使用了 `tkinter` 模块来创建图形用户界面,并使用了 `calendar` 模块来生成万年历。在用户输入年份和月份后,点击“Show”按钮将会在屏幕上显示所选月份的万年历。
阅读全文