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.
Label
after its complete definition was executed. Therefore atclass Label(Label)
the secondLabel
refers totkinter.Label
yet until the class is fully defined. This means the newLabel
class is derived fromtkinter.Label
. Of course this is very bad style.Toplevel
. #display = Toplevel() output = 0 ans = Label(root, output, 0, 0, 'yes') root.mainloop()