Why do you inherit the main Tk window? Why do you inherit the main Tk window? tkinter tkinter

Why do you inherit the main Tk window?


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.


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.