In my Tkinter projects I almost always end up with __init__()
methods that look like this:
import tkinter as tk
from .common import ConfigPageFrame
class SystemInformationPageFrame(ConfigPageFrame):
def __init__(self, parent, callback_dict):
super().__init__(parent)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.button_text = "Refresh"
self.button_callback = callback_dict['btn_refresh']
self.lbl_sys_desc_text = "System description:"
self.lbl_model_text = "Model:"
self.lbl_serial_text = "Serial number:"
self.lbl_mac_text = "Burned in MAC:"
self.lbl_soft_ver_text = "Software version:"
self.lbl_os_text = "Operating system:"
self.lbl_net_chip_text = "Network processing device:"
self.btn_refresh = tk.Button(self, text=button_text,
command=button_callback)
self.btn_refresh.grid(column=0, row=10, columnspan=2)
self.lbl_sys_desc = tk.Label(self, text=lbl_sys_desc_text, anchor=tk.E)
self.lbl_model = tk.Label(self, text=lbl_model_text, anchor=tk.E)
self.lbl_serial = tk.Label(self, text=lbl_serial_text, anchor=tk.E)
self.lbl_mac = tk.Label(self, text=lbl_mac_text, anchor=tk.E)
self.lbl_soft_ver = tk.Label(self, text=lbl_soft_ver_text, anchor=tk.E)
self.lbl_os = tk.Label(self, text=lbl_os_text, anchor=tk.E)
self.lbl_net_chip = tk.Label(self, text=lbl_net_chip_text, anchor=tk.E)
self.lbl_sys_desc.grid(column=0, row=0, sticky=tk.E)
self.lbl_model.grid(column=0, row=1, sticky=tk.E)
self.lbl_serial.grid(column=0, row=2, sticky=tk.E)
self.lbl_mac.grid(column=0, row=3, sticky=tk.E)
self.lbl_soft_ver.grid(column=0, row=4, sticky=tk.E)
self.lbl_os.grid(column=0, row=5, sticky=tk.E)
self.lbl_net_chip.grid(column=0, row=6, sticky=tk.E)
This sample is a GUI class from quite a large project, so I can't really show you the whole code (1700+ lines right now, tending towards much more).
As you can see, the overall structure of the widgets instantiation is very repetitive.
Is there a better way to widget instantiation? Maybe some less repetitive approach that doesn't seem to violate DRY?