carlosdlcf
carlosdlcf

Reputation: 25

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 (i.e. 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)

Upvotes: 0

Views: 3643

Answers (1)

figbeam
figbeam

Reputation: 7176

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.

Upvotes: 1

Related Questions