Search code examples
pythonpython-3.xclasstkintersuper

no idea where tkinter label is being defined in class


I found this code for a label class for a tkinter popup I was making. I didn't understand how the label appeared, but I just went on with it. However later I found that I needed to refer to the created label in order to bind to it. I have no idea what line is the actual place the label is being created, so when I was trying to assign the label to a variable, I didn't know where. Can someone please explain what is happening in this code and how I can call back to the label I just created inside the class?

I figured out the label is being created somewhere where super() is being used and has something to do with the Label parameter being passed directly to the class. However after researching what super().__init__() did I figured it just allows classes to use parameters from other classes, so I'm still confused on how it works when its referring to itself. I also am confused on where the parameters passed directly into the class go (rather that being in the __init__)

I tried assigning the super() to a variable, or just using Label as a variable, or removing the self. at the beginning.

I made a mini version of the class to see what was going on but to no success. Here is the code:

from tkinter import *


class Label(Label):
    def __init__(self, root, text, row, col, tag):
        self.text = text
        self.row = row
        self.column = col
        self.root = root
        self.tag = tag
        super().__init__(root)
        self['text'] = self.text
        self.grid(row=self.row, column=self.column)
        # self.label.bind("<Configure>", self.move) # this is the line of code im trying to get to work, but    cant because i dont know where the label is being created. it doesnt work           
    def move(self):
        print('moving')

root = Tk()
display = Toplevel()
output = 0
ans = Label(display, output, 0, 0, 'yes')
root.mainloop()

Sorry if this is a bad question and or it is answered somewhere else this is my first question on here and I'm pretty sure theres another easy solution but I just really want to understand what is happening here.


Solution

  • I have no idea what line is the actual place the label is being created

    It is this line:

    ans = Label(display, output, 0, 0, 'yes')
    

    That is because Label is a superclass of tkinter's Label class, so any instance of this class is a label. Within the class the actual label object is referred to with self and is created when this line executes:

    super().__init__(root)
    

    You don't need to assign the result of super().__init__(root) to a variable. That is handled automatically by python.

    However later I found that I needed to refer to the created label in order to bind to it.

    You should bind to self:

    self.bind("<Configure>", self.move)
    

    1 You should not be naming a custom class the same as an imported class. That will make the code more confusing. I recommend that you rename your Label class to something else, such as CustomLabel.