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

Time:08-06

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()

CodePudding user response:

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.

  • Related