如何根据另一个Combobox的选择填充Combobox?

时间:2015-06-09 15:21:44

标签: python-3.x combobox tkinter

我正在尝试根据另一个组合框的选择来填充组合框。如果我使用更新按钮来更新组合框#1选择,我可以这样做。但是,我想动态地进行更新。

我附上了一个简单的代码示例,除了一个问题外,它似乎按预期工作。您必须两次选择组合框#1才能更新组合框#2。如果有人对如何解决这个问题有任何想法,我将深表感谢。由于我通过示例学习,请您提供代码示例。

提前谢谢你: begez

from tkinter import *
import tkinter.ttk,csv

global CategoryCombo

def getUpdateData():

    cat = CategoryCombo.get()
    if cat == 'car':
        AccountCombo = tkinter.ttk.Combobox( width = 15,value = car)
        AccountCombo.grid(row = 5,column = 1,pady = 25,sticky = E)
    elif cat == 'home':
        AccountCombo = tkinter.ttk.Combobox( width = 15,value = home)
        AccountCombo.grid(row = 5,column = 1,pady = 25,sticky = E)
    elif cat == 'rv':
        AccountCombo = tkinter.ttk.Combobox( width = 15,value = rv)
        AccountCombo.grid(row = 5,column = 1,pady = 25,sticky = E)


category = ['home','car','rv']

home = ['utilities','rent','cable']

car = ['gas','oil','repairs']

rv = ['parks','maintenance','payment']
v = StringVar

root = tkinter.Tk()

class Application(Frame):

    def __init__(self, master=None, Frame=None):
        Frame.__init__(self, master)
        super(Application,self).__init__()
        self.grid(column = 5,row = 20,padx = 50,pady = 50)
        self.createWidgets()


    def createWidgets(self):
        global CategoryCombo

        CatCBLabel = Label(text = 'Combo Box #1:').grid(row = 2,column = 1,padx = 10)
        ActCBLabel = Label(text = 'Combo Box #2:').grid(row = 4,column = 1,padx = 10)
        AccountCombo = tkinter.ttk.Combobox( width = 15)
        AccountCombo.grid(row = 5,column = 1,pady = 25,padx = 10)
        CategoryCombo = tkinter.ttk.Combobox(width = 15,values = category,textvariable = v,postcommand = getUpdateData)
        CategoryCombo.grid(row = 3,column = 1,padx = 10,pady = 25)
        print(v)



app = Application()

app.master.title('Yearly Budget Setup')

app.mainloop()

1 个答案:

答案 0 :(得分:3)

  1. 不要使用全局变量。改为使用类变量。
  2. 使用词典而不是多个变量。
  3. 使用ComboboxSelected事件
  4. 结果:

    from tkinter import *
    import tkinter.ttk
    
    category = {'home': ['utilities','rent','cable'],
        'car': ['gas','oil','repairs'],
        'rv':['parks','maintenance','payment']}
    
    class Application(Frame):
    
        def __init__(self, master=None, Frame=None):
            Frame.__init__(self, master)
            super(Application,self).__init__()
            self.grid(column = 5,row = 20,padx = 50,pady = 50)
            self.createWidgets()
    
        def getUpdateData(self,  event):
            self.AccountCombo['values'] = category[self.CategoryCombo.get()]
    
        def createWidgets(self):
            Label(text = 'Combo Box #1:').grid(row = 2,column = 1,padx = 10)
            Label(text = 'Combo Box #2:').grid(row = 4,column = 1,padx = 10)
            self.AccountCombo = tkinter.ttk.Combobox( width = 15)
            self.AccountCombo.grid(row = 5,column = 1,pady = 25,padx = 10)
    
            self.CategoryCombo = tkinter.ttk.Combobox(width = 15,  values = list(category.keys()))
            self.CategoryCombo.bind('<<ComboboxSelected>>', self.getUpdateData)
            self.CategoryCombo.grid(row = 3,column = 1,padx = 10,pady = 25)
    
    app = Application()
    app.master.title('Yearly Budget Setup')
    app.mainloop()