使用 tkinter 创建日历的最佳方法是啥?
Posted
技术标签:
【中文标题】使用 tkinter 创建日历的最佳方法是啥?【英文标题】:What would be the best way to create a calender using tkinter?使用 tkinter 创建日历的最佳方法是什么? 【发布时间】:2020-03-02 19:47:55 【问题描述】:我正在为学校做一个项目,这是一个系统,员工可以输入他们的姓名,然后输入他们的假期日期,这将显示在日历上。管理员还可以添加假期限制、删除假期以及添加/删除员工。 我在这个系统中使用 tkinter,我有一个访问数据库,可以存储工作人员、假期日期等。 使用 tkinter 创建日历的最佳方法是什么?它将需要在日期标记上显示假期,并可能在该日期显示谁将在假期中的姓名。 一般的任何建议也将不胜感激:)谢谢:) 如果有帮助,这是我目前的:
import tkinter as tk
from tkinter import END, PhotoImage
LARGE_FONT = ("Verdana", 12)
LARGER_FONT = ("Comic Sans MS", 20)
TITLE_FONT = ("Verdana", 15)
class HolSys(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames =
for F in (StartPage, PageOne, PageTwo, AdminSelection, StaffAddition, HolidayRemoval, HelpPage, RestrictionAddition):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.winfo_toplevel().title("Holiday System")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
label2 = tk.Label(self, text="Starter", font=LARGE_FONT).grid(
row=1, column=1, pady=10, padx=10)
button1 = tk.Button(self, text="Holiday Input", command=lambda: controller.show_frame(
PageOne), width=14, height=2).grid(row=2, column=1)
button2 = tk.Button(self, text="Admin", command=lambda: controller.show_frame(
PageTwo), width=14, height=2).grid(row=2, column=2)
buttonHelp = tk.Button(self, text="?", command=lambda: controller.show_frame(
HelpPage), font=LARGER_FONT, width=1, height=1).grid(row=0, column=0)
class HelpPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text="Help", font=TITLE_FONT).grid()
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.grid()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
month = tk.StringVar(self)
year = tk.StringVar(self)
monthList = [
'pick a month',
'Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec'
]
yearList = ['2019', '2020', '2021', '2022']
month31 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"]
month30 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"]
month29 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"]
month28 = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14",
"15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28"]
MonthsWith31D = ['Jan', 'Mar', 'May', 'Jul', 'Aug', 'Oct', 'Dec']
MonthsWith30D = ['Apr', 'Jun', 'Sep', 'Nov']
LeapYears = ['2020', '2024', '2028']
Mvar = tk.StringVar(self)
Mvar.set("January") # initial value for month
Dvar = tk.StringVar(self)
Dvar.set("1st") # initial value for date
Yvar = tk.StringVar(self)
Yvar.set("2019") # initial value for year
s_month = month31
date = tk.OptionMenu(self, Dvar, *s_month)
date.pack() # creates the optionmenu button for dates
def MonthSelEvent(*args):
if month.get() == 'Feb':
date['menu'].delete(0, 'end')
if year.get() in LeapYears:
date['menu'].delete(0, 'end')
for x in month29:
date['menu'].add_command(
label=x, command=tk._setit(Dvar, x))
# set it to first/default for selection
Dvar.set(month29[0])
else:
date['menu'].delete(0, 'end')
for x in month28:
date['menu'].add_command(
label=x, command=tk._setit(Dvar, x))
# set it to first/default for selection
Dvar.set(month28[0])
elif month.get() in MonthsWith31D:
date['menu'].delete(0, 'end')
for x in month31:
date['menu'].add_command(
label=x, command=tk._setit(Dvar, x))
# set it to first/default for selection
Dvar.set(month31[0])
elif month.get() in MonthsWith30D:
date['menu'].delete(0, 'end')
for x in month30:
date['menu'].add_command(
label=x, command=tk._setit(Dvar, x))
# set it to first/default for selection
Dvar.set(month30[0])
else:
pass # covers the 'select a month' option
monthSelect = tk.OptionMenu(self, month, *monthList,
command=MonthSelEvent).pack()
# trace on the variable that is changed
month.trace("w", MonthSelEvent)
yearSelect = tk.OptionMenu(
self, year, *yearList, command=MonthSelEvent).pack()
year.trace('w', MonthSelEvent)
year.set(yearList[0])
month.set(monthList[0]) # set initial value of dropdown
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Admin", font=LARGE_FONT)
L1 = tk.Label(self, text="Password").grid(row=0)
e1 = tk.Entry(self, show="*")
e1.grid(row=0, column=1)
def callback(event=None):
if e1.get() == "john":
controller.show_frame(AdminSelection)
e1.delete(0, END)
e1.insert(0, "")
else:
L2.config(text="incorrect")
e1.delete(0, END)
e1.insert(0, "")
L2 = tk.Label(self, text="")
L2.grid()
button1 = tk.Button(self, text="OK", command=callback).grid()
self.bind_all("<Return>", callback)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage)).grid()
class AdminSelection(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text="sweet").grid()
tk.Button(self, text="Staff\n Removal/Addition",
command=lambda: controller.show_frame(StaffAddition)).grid(column=0)
tk.Button(self, text="Holiday Restriction/Addition",
command=lambda: controller.show_frame(RestrictionAddition)).grid(column=1)
tk.Button(self, text="Holiday\n Removal", command=lambda: controller.show_frame(
HolidayRemoval)).grid(column=1, row=1)
tk.Button(self, text="Back to Home", command=lambda: controller.show_frame(
StartPage)).grid(column=3)
class StaffAddition(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text="Staff Addition").grid()
tk.Button(self, text="Back", command=lambda: controller.show_frame(
AdminSelection)).grid(column=3)
class RestrictionAddition(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text="Restriction Addition").grid()
tk.Button(self, text="Back", command=lambda: controller.show_frame(
AdminSelection)).grid(column=3)
class HolidayRemoval(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
tk.Label(self, text="Holiday Removal").grid()
tk.Button(self, text="Back", command=lambda: controller.show_frame(
AdminSelection)).grid(column=3)
app = HolSys()
app.mainloop()
【问题讨论】:
tkcalendar - example 【参考方案1】:有tkcalendar
如果您从文档中运行example,那么您可以看到日历
和事件
或选择日期
【讨论】:
以上是关于使用 tkinter 创建日历的最佳方法是啥?的主要内容,如果未能解决你的问题,请参考以下文章