from tkinter import *
from tkinter import ttk
import calendar as cld
import datetime as dt
win = Tk()
win.title("简单的万年历")
win.geometry("500x400")
lb1 = Label(win, text="年:")
lb2 = Label(win, text="月:")
year_range = [i for i in range(1970, 2050)]
month_range = [i for i in range(1, 13)]
cbb_year = ttk.Combobox(win, values=year_range)
cbb_month = ttk.Combobox(win, values=month_range)
cbb_year.current(year_range.index(dt.datetime.now().year))
cbb_month.current(month_range.index(dt.datetime.now().month))
lb1.grid(row=0, column=0, padx=5, pady=10)
cbb_year.grid(row=0, column=1, padx=5, pady=10)
lb2.grid(row=0, column=2, padx=5, pady=10)
cbb_month.grid(row=0, column=3, padx=5, pady=10)
# 计算和显示
# c = cld.month(2021, 10)
# lb_cld = Label(win, text=c, )
# lb_cld.grid(row=1, column=0, columnspan=4)
# Lable格式有点问题。
# HTMLLabel 不支持tb等标签。
# 用Text组件可以用,就将就着用它吧。。
def setYM():
"""指定年月"""
year = int(cbb_year.get())
month = int(cbb_month.get())
c = cld.TextCalendar(cld.SUNDAY)
print(c.formatmonth(year, month))
txt.delete("1.0", END)
txt.insert("1.0", c.formatmonth(year, month))
txt = Text(win, width=50, height=20)
c = cld.TextCalendar(cld.SUNDAY) # 周日为开头
txt.insert("1.0", c.formatmonth(2021, 10))
txt.grid(row=2, column=0, columnspan=4)
btn = Button(win, text="确定", command=setYM)
btn.grid(row=0, column=4, padx=5, pady=10)
win.mainloop()
|