简体   繁体   English

Python Tkinter-如何根据OptionMenu选择更新组合框值?

[英]Python Tkinter - How to update Combobox values depending on OptionMenu Selection?

I'm looking for some help with the following. 我在以下方面寻求帮助。

I'm working on a small project that requires the ComboBox values to be updated depending on the selection the user makes in an OptionMenu. 我正在做一个小型项目,该项目需要根据用户在OptionMenu中所做的选择来更新ComboBox值。

Currently the Combo Box shows the values for Thread 1 but for most of the time it shows a value like PY with a number (ie PY_VAR2) 当前,“组合框”显示线程1的值,但在大多数情况下,它显示带有数字的PY之类的值(即PY_VAR2)

Below is the main section of the code from these two widgets I'm trying to connect. 以下是我尝试连接的这两个小部件中代码的主要部分。

Thanks in advance for your help. 在此先感谢您的帮助。

### 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)

Combo Box Section 组合框部分

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)

Well, you have a callback from the OptionMenu: thdTypeSelection so just update Combobox there: 好吧,您从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'])

It bothers me a bit that Thread 1 is already selected in the OptionMenu but the Combobox presents TPIVals , whatever they might be. 令我有些困扰的是,虽然已经在OptionMenu中选择了Thread 1 ,但是组合TPIVals显示了TPIVals ,无论它们是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM