Tkinter 如何根据此组合框自动更新第二个组合框

EMi*_*iTu 5 python combobox tkinter

我在 Tkinter Python 中遇到了组合框更新的问题。

我有两个组合框:

  • A带有values =['A','B','C']和 的组合框
  • 组合框B

我想要的是:

  • A当在组合框中选择值时A,然后在组合框中B显示值['1','2','3']

  • B当在组合框中选择值时A,然后在组合框中B显示值['11','12','13']

  • C当在组合框中选择值时A,然后在组合框中B显示值 s['111','112','113']

目前我的部分代码如下:

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')
Run Code Online (Sandbox Code Playgroud)

acw*_*668 6

实际上你不需要全局变量ListB。并且您需要comboboxB.config(values=...)在末尾添加CallHotel()来设置以下选项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)
Run Code Online (Sandbox Code Playgroud)

comboboxB并直接将的初始值更改为ListB1

comboboxB=ttk.Combobox(win0,textvariable=stp,values=ListB1,width=15)
Run Code Online (Sandbox Code Playgroud)