1

The code i have below is from sentdex's tutorials on youtube for a python application using the below as an example why would you have a class inherit your main window (class Menu(tk.Tk))?

import tkinter as tk

Font = ("Verdana", 12)

class Menu(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        tk. Tk.iconbitmap(self, default="kali_icon.ico")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.Menus = {}

        for S in (MenuOne, MenuTwo):
            sub = S(container, self)

            self.Menus[S] = sub

            sub.grid(row=0, column=0, sticky="nsew")

        self.show_frame(MenuOne)

    def show_frame(self, cont):
        frame = self.Menus[cont]
        frame.tkraise()

class MenuOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page 1")
        label.pack(pady=10, padx=10)
        tk.Button(self, text="Page 2", command=lambda: controller.show_frame(MenuTwo)).pack()


class MenuTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page 2")
        label.pack(pady=10, padx=10)
        tk.Button(self, text="Back to Page 1", command=lambda: controller.show_frame(MenuOne)).pack()

app = Menu()
app.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Chris James
  • 151
  • 2
  • 14

2 Answers2

1

Every application needs a root window. It's a natural choice to extend this root window with application-specific data. It's not the only choice, however, It is perfectly reasonable to have a main application class that uses composition (creates an instance of tk.Tk) rather than inheritance.

There's no real advantage to either solution. It's just personal preference.

The tutorial you reference is probably that way because he copied it from the accepted answer to this stackoverflow question: Switch between two frames in tkinter. The accepted answer is one that I wrote. At the time, it seemed like the simplest way for me to provide an example.

Personally, if I were writing real production code -- versus a concise example for illustrative purposes -- I would likely choose to separate the controller from the root window. Though, in that case I might still inherit from tk.Tk for the purposes of creating the window, but the business logic would likely live in a separate class.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • As an additional question in relation to the same code in the scenario where you would want to reference to the container class inside __init__ from another module wouldn't this be a disadvantage of this approach? – Chris James Dec 30 '19 at 19:20
  • @ChrisJames: I don't see why this structure would be a disadvantage. The choice is to have two objects (the root window and an instance of the main application class), or a single class that does both. Regardless, accessing private members of either class is more-or-less the same. Though, if you want to follow a strict model/view/controller architecture it makes sense to separate them into two different classes. – Bryan Oakley Dec 30 '19 at 19:22
0

you have a class inherit your main window

Noone inherits your main window. It your class that inherits tk.Tk which in turns provides your all necessary stuff for your widgets to work, without requiring you to write all the code:

The Tk class is instantiated without arguments. This creates a toplevel widget of Tk which usually is the main window of an application. Each instance has its own associated Tcl interpreter.

https://docs.python.org/3/library/tkinter.html

When using framework (of any sort and on any plaftorm) it's quite common practice for the framework to provide some sort of foundation/base classes and requiring developers to extend them to achieve required functionality. Thanks to this approach, you only need to focus on your stuff.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141