Tkinter how to update second combobox automatically according this combobox

ε祈祈猫儿з 提交于 2020-01-23 17:39:08

问题


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

I have two comboboxes:

  • combobox A with values =['A','B','C'] and
  • combobox B

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')

回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!