Reputation: 51
I have encountered an issue with combobox updates in Tkinter Python.
I have two comboboxes:
A
with values =['A','B','C']
andB
What i want is that:
when value A
is selected in combobox A
then in combobox B
show the values ['1','2','3']
when value B
is selected in combobox A
then in combobox B
show the values ['11','12','13']
when value C
is selected in combobox A
then in combobox B
show the value s ['111','112','113']
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
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