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

Python/Tkinter: root as a class / initializing instance variables in a tkinter class

I have created a custom tkinter root window with customized title bar and other modifications, as a class. The class is saved in a module named ModRoot. The relevent code is as follows:

import tkinter as tk

class Rt(tk.Tk):
  def __init__(self,winhght,winwdth,apptitle):
    super().__init__()

    # initialize instance variables
    self.winhght = winhght
    self.winwdth = winwdth
    self.apptitle = apptitle

In the main application, this root window is created with two lines of code, and the addition of the mainloop.

import ModRoot
RootWindow = ModRoot.Rt(400,800,"App Title")
RootWindow.mainloop()

I have two questions:

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

First, this root window works fine but, as I have learned in the past, there are sometimes problems with a particular approach that are not immediately evident. So the first question is simply whether or not the above approach to creating a root window is acceptable and correct.

The second question has to do with initializing the instance variables (self.winhght = winhght and so on). Strangely enough, I have found that if I comment these lines out, the class works just as it did before. Yet, every tutorial on classes indicates that these variables must be initialized. If that’s the case, why is this class working with those lines commented out? And are they actually necessary?

>Solution :

So the first question is simply whether or not the above approach to creating a root window is acceptable and correct.

Yes, it’s acceptable.

Yet, every tutorial on classes indicates that these variables must be initialized. If that’s the case, why is this class working with those lines commented out? And are they actually necessary?

It’s impossible for us to say. Do you use self.winwdth, self.winhght, and self.apptitle anywhere else in the code? You only need to initialize them if you need them later.

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