简体   繁体   中英

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.

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)

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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