这应该很简单,但我真的很难把它做好。 我只需要一个简单的 ttk ComboBox,它会根据选择的变化更新一个变量。
在下面的示例中,我需要每次进行新选择时自动更新 value_of_combo
变量的值。
from Tkinter import *
import ttk
class App:
value_of_combo = 'X'
def __init__(self, parent):
self.parent = parent
self.combo()
def combo(self):
self.box_value = StringVar()
self.box = ttk.Combobox(self.parent, textvariable=self.box_value)
self.box['values'] = ('X', 'Y', 'Z')
self.box.current(0)
self.box.grid(column=0, row=0)
if __name__ == '__main__':
root = Tk()
app = App(root)
root.mainloop()
最佳答案
只需绑定(bind)虚拟事件<<ComboboxSelected>>
到组合框小部件:
class App:
def __init__(self, parent):
self.parent = parent
self.value_of_combo = 'X'
self.combo()
def newselection(self, event):
self.value_of_combo = self.box.get()
print(self.value_of_combo)
def combo(self):
self.box_value = StringVar()
self.box = ttk.Combobox(self.parent, textvariable=self.box_value)
self.box.bind("<<ComboboxSelected>>", self.newselection)
# ...
关于python - 简单的ttk组合框演示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17757451/