I'm writing software which allows a user to view data in a number of different formats, and they can switch between formats at any time. I'm wondering if there's a better way to do this than switching between subclasses, and if not, if there's a better way to write this than I have.
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() The program initializes a window with a selection of formats to view the data, and shows the data in some default format. Each subclass of Display only overrides the show() method.
This isn't the entire program I'm working on, but the important part is that the subclasses of Display don't actually initialize themselves, and only override certain parent methods. Is there a better way to change the way the data is displayed than this?
