Python Tkinter - 如何根据 OptionMenu 选择更新组合框值?
Python Tkinter - How to update Combobox values depending on OptionMenu Selection?
我正在寻求以下方面的帮助。
我正在做一个小项目,该项目需要根据用户在 OptionMenu 中所做的选择来更新 ComboBox 值。
目前,组合框显示线程 1 的值,但在大多数情况下,它显示的值类似于带有数字的 PY(即 PY_VAR2)
下面是我尝试连接的这两个小部件的代码的主要部分。
在此先感谢您的帮助。
### Option Menu Section
thdTypeLabel = Label(thdParamsFrame, text="Thread Type")
thdTypeLabel.grid(row=0, column=0, padx=(30,10), pady=(10,10),sticky=E)
thdInitType = StringVar(thdParamsFrame)
thdInitType.set("Thread 1")
thdTypeMenu = OptionMenu(thdParamsFrame, thdInitType, "Thread 1","Thread 2", "Thread 3", command=thdTypeSelection)
thdTypeMenu.grid(row=0, column=1)
thdTypeMenu.configure(width=14)
组合框部分
thdInitTPI = StringVar()
thdTPICombo = ttk.Combobox(thdParamsFrame, width = 17, textvariable=thdInitTPI, values=TPIVals)
thdType = thdInitType.get()
if thdType == "Thread 1":
thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
elif thdType == "Thread 2":
thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
elif thdType =="Thread 3":
thdTPICombo.config(values=['6','7','8','10','11','12','14','16','18','20'])
thdTPICombo.bind('<<ComboboxSelected>>',None)
好吧,您有一个来自 OptionMenu 的回调:thdTypeSelection
所以只需在那里更新 Combobox:
def thdTypeSelection(event=None):
thdType = thdInitType.get()
if thdType == "Thread 1":
thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
elif thdType == "Thread 2":
thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
elif thdType =="Thread 3":
thdTPICombo.config(values=['6','7','8','10','11','12','14','16','18','20'])
让我有点困扰的是 Thread 1
已经在 OptionMenu 中被选中,但是 Combobox 显示 TPIVals
,无论它们是什么。
我正在寻求以下方面的帮助。
我正在做一个小项目,该项目需要根据用户在 OptionMenu 中所做的选择来更新 ComboBox 值。
目前,组合框显示线程 1 的值,但在大多数情况下,它显示的值类似于带有数字的 PY(即 PY_VAR2)
下面是我尝试连接的这两个小部件的代码的主要部分。
在此先感谢您的帮助。
### Option Menu Section
thdTypeLabel = Label(thdParamsFrame, text="Thread Type")
thdTypeLabel.grid(row=0, column=0, padx=(30,10), pady=(10,10),sticky=E)
thdInitType = StringVar(thdParamsFrame)
thdInitType.set("Thread 1")
thdTypeMenu = OptionMenu(thdParamsFrame, thdInitType, "Thread 1","Thread 2", "Thread 3", command=thdTypeSelection)
thdTypeMenu.grid(row=0, column=1)
thdTypeMenu.configure(width=14)
组合框部分
thdInitTPI = StringVar()
thdTPICombo = ttk.Combobox(thdParamsFrame, width = 17, textvariable=thdInitTPI, values=TPIVals)
thdType = thdInitType.get()
if thdType == "Thread 1":
thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
elif thdType == "Thread 2":
thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
elif thdType =="Thread 3":
thdTPICombo.config(values=['6','7','8','10','11','12','14','16','18','20'])
thdTPICombo.bind('<<ComboboxSelected>>',None)
好吧,您有一个来自 OptionMenu 的回调:thdTypeSelection
所以只需在那里更新 Combobox:
def thdTypeSelection(event=None):
thdType = thdInitType.get()
if thdType == "Thread 1":
thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
elif thdType == "Thread 2":
thdTPICombo.config(values=['2','3','4','5','6','8','10','12','14','16'])
elif thdType =="Thread 3":
thdTPICombo.config(values=['6','7','8','10','11','12','14','16','18','20'])
让我有点困扰的是 Thread 1
已经在 OptionMenu 中被选中,但是 Combobox 显示 TPIVals
,无论它们是什么。