【问题标题】:Tkinter how to update second combobox automatically according this comboboxTkinter 如何根据此组合框自动更新第二个组合框
【发布时间】:2016-10-19 08:49:31
【问题描述】:

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

我有两个组合框:

  • 组合框Avalues =['A','B','C']
  • 组合框B

我想要的是:

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

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

  • 当在组合框A 中选择值C 时,在组合框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')

【问题讨论】:

  • 有什么问题?你收到错误信息吗?显示有问题。

标签: python tkinter combobox


【解决方案1】:

其实你不需要全局变量ListB。并且需要在CallHotel()末尾加上comboboxB.config(values=...)来设置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)

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

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

【讨论】:

  • 感谢您的回答,它没有显示任何错误,只是没有按我的预期更新列表。
  • 现在,我使用了配置功能,所以它解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 2018-11-07
  • 1970-01-01
相关资源
最近更新 更多