问题
I have encountered an issue with combobox updates in Tkinter Python.
I have two comboboxes:
- combobox
A
withvalues =['A','B','C']
and - combobox
B
What i want is that:
when value
A
is selected in comboboxA
then in comboboxB
show the values['1','2','3']
when value
B
is selected in comboboxA
then in comboboxB
show the values['11','12','13']
when value
C
is selected in comboboxA
then in comboboxB
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')
回答1:
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)
来源:https://stackoverflow.com/questions/40126449/tkinter-how-to-update-second-combobox-automatically-according-this-combobox