使用tkinter创建压光机的最佳方法是什么?

时间:2020-03-02 19:47:55

标签: python ms-access tkinter

我正在为一个学校项目工作,这是一个系统,员工可以在该系统中输入他们的姓名,然后输入他们的假期日期,这将显示在日历上。管理员还可以添加假期限制,删除假期以及添加/删除工作人员。 我正在为此系统使用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()

1 个答案:

答案 0 :(得分:0)

tkcalendar

如果您从文档中运行example,则可以看到日历

enter image description here

以及事件

enter image description here

或选择日期

enter image description here

相关问题