Reputation: 127
Good day. I was confused when creating a radiobutton or any kind of widget like the label widget inside a class method because it was not stored in a some kind of container like a variable. It's my first time seeing this kind of code: here it is
class Application(Frame):
""" GUI Application for favorite movie type. """
def __init__(self, master):
""" Initiale Frame. """
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
""" Create widgets for movie type choices. """
# Create description label
Label(self,
text = "Choose your favorite type of movie"
).grid(row=0, column=0, sticky= W)
# create instruction label
Label(self,
text="Select one:"
).grid(row=1,column=0, sticky=W)
Radiobutton(self,
text="Comedy",
variable=self.favorite,
value = "comedy.",
command = self.update_text
).grid(row = 2, column = 0, sticky=W)
# create Drama radio button
Radiobutton(self,
text = "Drama",
variable = self.favorite,
value = "drama.",
command = self.update_text
).grid(row = 3, column = 0, sticky = W)
# create Romance button
Radiobutton(self,
text = "Romance",
variable = self.favorite,
value = "romance.",
command = self.update_text
).grid(row = 4, column = 0, sticky = W)
I usually see codes like this:
radio = Radiobutton(root)
radio.grid()
Can you explain me what happen with the first code? How did it create a widget without storing it in a some kind of a variable like in a second code
Upvotes: 0
Views: 598
Reputation: 385970
Tkinter is just a thin wrapper around an embedded Tcl interpreter which itself has the tk toolkit loaded. When you create a widget, the actual widget is represented as an object inside the tcl interpreter.
Tkinter will create a python object that holds a reference to this tcl object, but the existence of the python object isn't required for the tcl/tk object to exist. If you create a widget without saving a reference, the widget is still created and still exists in the tcl/tk interpreter.
Upvotes: 1