User Tools

Site Tools


programming-languages:tkinter:class_structure

Class structure in GUI

A GUI should structured in classes as follows:

  1. base class:
    • class Gui(tk.Frame);
  2. classes for widgets that make a macro-structure and that rely on the base class, like:
    • class Toplevel(Gui);
    • class Stautsbar(Gui);
    • class Toolbar(Gui).

Their initialization is described in the following example:

#!/usr/bin/env python3
 
import core  # calculation methods 
import tkinter as tk
 
 
class Popup(tk.Toplevel):
    def __init__(self):
        """Pop-up window for messages."""
        tk.Toplevel.__init__(self)
        self.title("New Window")
 
        self.b = tk.Button(self, text="Close", command=destroy)
        self.b.grid(row=0, column=0)
 
 
class Gui(tk.Frame):
    def __init__(self, master):
        """Initialize the GUI."""
        tk.Frame.__init__(self)
        self.grid()
 
        # toolbar
        self.toolbar = Toolbar(master)  # class instance
        self.toolbar.f_toolbar.grid(row=0, column=0)
 
        # central frame
        self.f = tk.Frame(master)
        self.f.grid(row=1, column=0)
 
        # statusbar
        self.statusbar = Statusbar(master)  # class instance
        self.statusbar.f_status.grid(row=2, column=0)
 
        # call method to build home window
        self.home()
 
    def home(self):
        pass
 
 
class Toolbar(Gui):
    def __init__(self, master):
        self.f_toolbar = tk.Frame(master)
        # ...
 
 
class Statusbar(Gui):
    def __init__(self, master):
        self.f_status = tk.Frame(master)
        # ...
 
 
if __name__ == "__main__":
    core = core.Core()
 
    root = tk.Tk()
 
    app = Gui(master=root)  # class instance
    app.master.title("Title")
    app.mainloop()

The new module ttk extends the tk's widgets and gives the possibility to control the widget aspect using style configurations, as in the following example.

tkinter-1.py
#!/usr/bin/env python3
 
import tkinter as tk
import tkinter.ttk as ttk  # ttk widgets overwrite tk's ones
 
 
class App(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
        self.a = ttk.Style()
        self.b = ttk.Style()
        self.a.configure("a.TButton", foreground="red")
        self.b.configure("b.TButton", foreground="blue")
 
    def create_widgets(self):
        self.button = ttk.Button(self, style="b.TButton")
        self.button["text"] = "Hello World!"
        self.button["command"] = self.change_style
        self.button.grid(row=1, column=1, )
 
        btn_quit = ttk.Button(self, text="QUIT", command=root.destroy)
        btn_quit.grid(row=2, column=1)
 
    def change_style(self):
        current_style = self.button.cget("style")
        if (current_style == "a.TButton"):
            self.button.configure(style="b.TButton")
        else:
            self.button.configure(style="a.TButton")
 
 
if __name__ == "__main__":
    root = tk.Tk()
    app = App(master=root)
    app.master.title("Title")
    app.mainloop()
programming-languages/tkinter/class_structure.txt · Last modified: 2020/04/09 20:19 by tormec