美好的一天。當創建一個單選按鈕或任何類型的窗口小部件(如類方法中的標籤窗口小部件)時,我感到困惑,因爲它沒有像變量那樣存儲在某種容器中。這是我第一次看到這樣的代碼:這裏是Tkinter創建一個小部件,而不像一個變量存儲在某種容器中?
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)
我經常看到這樣的代碼:
radio = Radiobutton(root)
radio.grid()
你能解釋一下我的第一個代碼發生什麼事?它是如何創建一個小部件而不像第二個代碼那樣將其存儲在某種變量中的?
如果符合以下條件,您只需將標識符分配給標識符:1.這是唯一的參考,以防止它們被遺棄;或者2.如果你想在你的代碼中輕鬆訪問它們。顯然,在這種情況下,這些都不適用。 – jonrsharpe
@jonrsharpe謝謝。 –