【发布时间】:2020-10-28 08:04:54
【问题描述】:
我对 Tkinter 很陌生。当我在“颜色 GUI”上创建标签和组合框时,标签和组合框都显示没有问题。我遇到的问题是,当我尝试为该组合框定义回调时,它消失了。
我只是在尝试使用类、实例和方法来练习和理解。任何帮助,将不胜感激。为什么会这样?
class OOP:
def __init__(self):
self.win = tk.Tk()
self.win.title('Color GUI')
self.win.geometry('450x400')
# Label for choosing Color
self.colorlabel = ttk.Label(self.win, text = 'Select a color: ',
font =("Times New Roman", 10)).grid(column=0,
row =1,padx=10, pady=10, sticky='W')
# Callback for color combobox
def color_callback(self, eventObject):
self.color_choose = eventObject.widget.get()
if self.color_choose == 'Green':
print('Hello World')
# Combobox creation for choosing color
self.n = tk.StringVar
self.colorList = ['Green', 'Blue', 'Red', 'Orange']
self.colorchoose = ttk.Combobox(self.win, width=5, textvariable = self.n, value=self.colorList, state='readonly')
self.colorchoose.grid(column=1, row=1, sticky='W')
self.colorchoose.bind("<<ComboboxSelected>>", color_callback)
#======================================================================
# Start GUI
#======================================================================
oop = OOP()
oop.win.mainloop()
【问题讨论】:
标签: class user-interface tkinter combobox callback