I'm currently writing software that stores some data, and the user can view that data in a number of ways. The way I have it set up now, I have one superclass which initializes widgets to display that data, and each subclass overwrites a show()
method to display the data in a handful of ways.
The solution I have works, but I'm wondering if there's a better solution than the one I have. This may be a question for a different stack exchange site, and if it is then I'll take it there.
Here's essentially my solution:
import tkinter as tk
from tkinter import ttk
class Display(ttk.Frame):
def __init__(self, master=None):
ttk.Frame.__init__(self, master, relief='sunken', padding='20')
self.widgets = [
ttk.Label(self, text="Data Value #1"),
ttk.Label(self, text="Data Value #2"),
ttk.Label(self, text="Data Value #3"),
ttk.Label(self, text="Data Value #4"),
ttk.Label(self, text="Data Value #5"),
ttk.Label(self, text="Data Value #6"),
ttk.Label(self, text="Data Value #7"),
ttk.Label(self, text="Data Value #8"),
ttk.Label(self, text="Data Value #9"),
ttk.Label(self, text="Data Value #10"),
]
def show(self):
for i in self.widgets:
i.pack()
class Display1(Display):
def show(self):
for i, widget in enumerate(self.widgets):
if i % 2 == 0:
widget.pack()
class Display2(Display):
def show(self):
for i, widget in enumerate(self.widgets):
if i % 2 == 1:
widget.pack()
class Display3(Display):
def show(self):
for i, widget in enumerate(self.widgets):
if i > 4:
widget.pack()
class Display4(Display):
def show(self):
for i, widget in enumerate(self.widgets):
if i < 5:
widget.pack()
class Display5(Display):
def show(self):
for i, widget in enumerate(self.widgets):
self.widgets[-i].pack()
class Window(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
#
#
# Initialize buttons to select how to view the data.
self.selection = tk.StringVar(self, value="Option 1")
self.display_options = [
ttk.Radiobutton(self, text="Option 1", variable=self.selection, value="Option 1", command=self.change_display),
ttk.Radiobutton(self, text="Option 2", variable=self.selection, value="Option 2", command=self.change_display),
ttk.Radiobutton(self, text="Option 3", variable=self.selection, value="Option 3", command=self.change_display),
ttk.Radiobutton(self, text="Option 4", variable=self.selection, value="Option 4", command=self.change_display),
ttk.Radiobutton(self, text="Option 5", variable=self.selection, value="Option 5", command=self.change_display),
]
for i, button in enumerate(self.display_options):
button.grid(row=i, column=1, padx=20)
#
#
# Initialize the frame holding the data
self.data_frame = Display(self)
self.data_frame.grid(row=0, column=0, rowspan=10)
self.data_frame.show()
def change_display(self):
for i in self.data_frame.pack_slaves():
i.pack_forget()
self.data_frame.grid_forget()
match self.selection.get():
case "Option 1":
self.data_frame = Display1(self)
case "Option 2":
self.data_frame = Display2(self)
case "Option 3":
self.data_frame = Display3(self)
case "Option 4":
self.data_frame = Display4(self)
case "Option 5":
self.data_frame = Display5(self)
case _:
self.data_frame = Display(self)
self.data_frame.show()
self.data_frame.grid(row=0, column=0, rowspan=10)
if __name__ == '__main__':
win = Window()
win.mainloop()
This isn't exactly what the whole system does, the important part is that the show
method of each subclass is different.
Edit: I should also add that the data is being pulled from a database, and the format the data is in also needs to be stored in the database. Changing the way the data is shown is not purely visual as it will affect what other software I have written does with the data.