Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading