寻找一种有效的方法来重用一组小部件或替代方案

时间:2019-11-18 15:36:02

标签: python python-3.x user-interface tkinter

这是我现在所拥有的:

enter image description here

这是使用仅一个小部件的大量代码创建的:

date_label = tk.Label(date_entry_frame, text="Enter creation timestamp: ")
date_label.pack(side=tk.TOP)
label_0 = tk.Label(date_entry_frame, text='DD/MM/YY:')
label_0.pack(side=tk.LEFT)
entry_1 = tk.Entry(date_entry_frame, width=2, borderwidth=1)
label_1 = tk.Label(date_entry_frame, text='/', borderwidth=0)
entry_2 = tk.Entry(date_entry_frame, width=2, borderwidth=1)
label_2 = tk.Label(date_entry_frame, text='/', borderwidth=0)
entry_3 = tk.Entry(date_entry_frame, width=4, borderwidth=1)
# time entries and labels
label_3 = tk.Label(date_entry_frame, text='HH:MM', borderwidth=0)
entry_4 = tk.Entry(date_entry_frame, width=2, borderwidth=1)
label_4 = tk.Label(date_entry_frame, text=':', borderwidth=0)
entry_5 = tk.Entry(date_entry_frame, width=2, borderwidth=1)

# space management
entry_1.pack(side=tk.LEFT)
label_1.pack(side=tk.LEFT)
entry_2.pack(side=tk.LEFT)
label_2.pack(side=tk.LEFT)
entry_3.pack(side=tk.LEFT)
label_3.pack(side=tk.LEFT)
entry_4.pack(side=tk.LEFT)
label_4.pack(side=tk.LEFT)
entry_5.pack(side=tk.LEFT)

有其他替代方法吗?还是一种无需复制粘贴大量代码即可重用此方法的方法? 我曾经尝试过使用类,但无法弄清楚如何在类中使用管理空间。

1 个答案:

答案 0 :(得分:1)

对于tkinter来说,答案与使用任何一种语言的任何大块代码都没有什么不同:将代码移至函数或类中。

可以说,最好的解决方案是将其移动到继承自Frame小部件的类。这样一来,您就可以像一流的窗口小部件一样对待类的实例(例如:可以在它们上调用packplacegriddestroy)。

例如,您希望能够使用这样的类:

start = DateTimeEntry(root, "Enter start time", 1, 1, 2019, 0, 0)
end = DateTimeEntry(root, "Enter end time", 31, 12, 2019, 0, 0)

start.pack(side="top", fill="x")
end.pack(side="top", fill="x")

enter image description here 该类可能看起来像这样:

class DateTimeEntry(tk.Frame):
    def __init__(self, master, label, day=1, month=1, year=2000, hour=0, minute=0):
        super().__init__(master)

        date_label = tk.Label(self, text=label)
        label_0 = tk.Label(self, text='DD/MM/YY:')
        label_1 = tk.Label(self, text='/', borderwidth=0)
        label_2 = tk.Label(self, text='/', borderwidth=0)
        label_3 = tk.Label(self, text='HH:MM', borderwidth=0)
        label_4 = tk.Label(self, text=':', borderwidth=0)

        self.entry_day = tk.Entry(self, width=2, borderwidth=1)
        self.entry_month = tk.Entry(self, width=2, borderwidth=1)
        self.entry_year = tk.Entry(self, width=4, borderwidth=1)
        self.entry_hour = tk.Entry(self, width=2, borderwidth=1)
        self.entry_minute = tk.Entry(self, width=2, borderwidth=1)

        date_label.pack(side=tk.TOP, fill="x")
        label_0.pack(side=tk.LEFT)
        self.entry_day.pack(side=tk.LEFT)
        label_1.pack(side=tk.LEFT)
        self.entry_month.pack(side=tk.LEFT)
        label_2.pack(side=tk.LEFT)
        self.entry_year.pack(side=tk.LEFT)
        label_3.pack(side=tk.LEFT)
        self.entry_hour.pack(side=tk.LEFT)
        label_4.pack(side=tk.LEFT)
        self.entry_minute.pack(side=tk.LEFT)

        self.set(day, month, year, hour, minute)

    def get(self):
        return (
            (self.entry_day.get(), self.entry_month.get(), self.entry_year.get()),
            (self.entry_hour.get(), self.entry_minute.get())
        )

    def reset(self):
        self.entry_day.delete(0, "end")
        self.entry_month.delete(0, "end")
        self.entry_year.delete(0, "end")
        self.entry_hour.delete(0, "end")
        self.entry_minute.delete(0, "end")

    def set(self, day, month, year, hour, minute):
        self.reset()
        self.entry_day.insert(0, day)
        self.entry_month.insert(0, month)
        self.entry_year.insert(0, year)
        self.entry_hour.insert(0, hour)
        self.entry_minute.insert(0, minute)