Why is it that I have to initialize the class and tk.Tk?

Question:

I’m just starting to get into object oriented programming. So I know that ‘class App(tk.Tk)’ makes tk.Tk the parent. If the class App is the basically the same as Tk here, why do I have to initialize App and tk.Tk separately?

import tkinter as tk

class App(tk.Tk):

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


window = App()
window.mainloop()
Asked By: Usman

||

Answers:

Technically, you don’t. If App.__init__ isn’t defined, then the attribute lookup resolves to the __init__ method of the parent (tk.Tk).

But since you did define App.__init__, the only way tk.Tk.__init__ is called is if you call it explicitly. You should do so, to ensure that the object is appropriately initialized as an instance (via subclassing) of tk.Tk. Once that is done, you can proceed with your App-specific initialization.

Answered By: chepner
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.