EMiYiTu
EMiYiTu

Reputation: 51

Tkinter how to update second combobox automatically according this combobox

I have encountered an issue with combobox updates in Tkinter Python.

I have two comboboxes:

What i want is that:

Currently part of my code as follows:

def CallHotel(*args):
    global ListB
    if hotel.get()==ListA[0]
        ListB=ListB1
    if hotel.get()==ListA[1]
        ListB=ListB2
    if hotel.get()==ListA[2]
        ListB=ListB3

ListA=['A','B','C']

ListB1=['1','2','3']

ListB2=['11','12','13']

ListB3=['111','112','113']

ListB=ListB1

hotel = StringVar()
hotel.set('SBT')

comboboxA=ttk.Combobox(win0,textvariable=hotel,values=ListA,width=8)
comboboxA.bind("<<ComboboxSelected>>",CallHotel)
comboboxA.pack(side='left')  

stp = StringVar()
stp.set('STP')

comboboxB=ttk.Combobox(win0,textvariable=stp,values=ListB,width=15)
comboboxB.pack(side='left')

Upvotes: 5

Views: 7996

Answers (1)

acw1668
acw1668

Reputation: 46669

Actually you don't need the global variable ListB. And you need to add comboboxB.config(values=...) at the end of CallHotel() to set the options of comboboxB:

def CallHotel(*args):
    sel = hotel.get()
    if sel == ListA[0]:
        ListB = ListB1
    elif sel == ListA[1]:
        ListB = ListB2
    elif sel == ListA[2]:
        ListB = ListB3
    comboboxB.config(values=ListB)

And change the initial values of comboboxB to ListB1 directly:

comboboxB=ttk.Combobox(win0,textvariable=stp,values=ListB1,width=15)

Upvotes: 6

Related Questions