从选择中填充列表框,python

时间:2013-10-30 22:16:27

标签: macos checkbox python-3.x listbox osx-mountain-lion

大家好看,可能会有所帮助。我是Python的新手,也是一般的编码。我正在研究一个程序,它的基本概念是有一些列表,从一个列表中进行选择,然后根据第一个选择填充另一个列表。我已经能够创建GUI,我已经能够创建各种不同形式的“Listbox”,我已经能够按照教程显示如何检索“Listbox”的指示,但我没有能够获得第二个列表框以填写我为第一个框中所做的选择创建的列表。我也试过了复选框,我确实更接近那个,但仍然没有骰子。

下面提供的代码的工作原理是我可以将第二个列表打印到python shell中,但是当我在GUI中设置了一个列表框时,我无法将其填充到那里。什么都不会发生。而且我知道我在这段代码中没有列表框可以填充第二个列表。所以在这段代码中,我希望'品牌'选择,如果'苹果',从列表框中的'modelA'填充列表。或者,如果复选框实际上不在那里,并且'品牌'中的项目在他们自己的列表框中。任何方向都可能比我能做的更多。再次感谢。

Python 3.3.2 Mac OS X 10.8.5

    from tkinter import *

    #brand would be for first listbox or checkboxes
    brand = ['Apples','Roses', 'Sonic', 'Cats']

    #model* is to be filled into the 2nd listbox, after selection from brand
    modelA = ['Ants', 'Arrows', 'Amazing', 'Alex']
    modelR = ['Early', 'Second', 'Real']
    modelS= ['Funny', 'Funky']
    modelC= ['Cool', 'Daring', 'Double']

    #create checkboxes 
    def checkbutton_value():
        if(aCam.get()):
            print("Models are: ", modelA)

        if(rCam.get()):
            print("Models are: ", modelR)

        if(sCam.get()):
            print("Models are: ", modelS)

        if(cCam.get()):
            print("Models are: ", modelC)

    #create frame, and check checkbuttons state, print value of model
    root = Tk()
    aCam = IntVar()
    rCam = IntVar()
    sCam = IntVar()
    cCam = IntVar()

    #Checkbutton functions
    apples = Checkbutton(root, text = "Apples", variable=aCam, command=checkbutton_value)
    apples.pack(anchor="w")
    roses = Checkbutton(root, text = "Roses", variable=rCam, command=checkbutton_value)
    roses.pack(anchor="w")
    sonic = Checkbutton(root, text = "Sonic", variable=sCam, command=checkbutton_value)
    sonic.pack(anchor="w")
    cats = Checkbutton(root, text = "Cats", variable=cCam, command=checkbutton_value)
    cats.pack(anchor="w")

    #general stuff for GUI
    root.title('My Brand')
    root.geometry("800x300")
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

要根据另一个列表框的选择填充列表框,您需要将方法绑定到第一个列表框的选择。以下是使用汽车品牌/型号的示例:

import Tkinter


class Application(Tkinter.Frame):
    def __init__(self, master):
        Tkinter.Frame.__init__(self, master)
        self.master.minsize(width=512, height=256)
        self.master.config()
        self.pack()

        self.main_frame = Tkinter.Frame()
        self.main_frame.pack(fill='both', expand=True)

        self.data = {
            'Toyota': ['Camry', 'Corolla', 'Prius'],
            'Ford': ['Fusion', 'Focus', 'Fiesta'],
            'Volkswagen': ['Passat', 'Jetta', 'Beetle'],
            'Honda': ['Accord', 'Civic', 'Insight']
        }

        self.make_listbox = Tkinter.Listbox(self.main_frame)
        self.make_listbox.pack(fill='both', expand=True, side=Tkinter.LEFT)

        # here we bind the make listbox selection to our method
        self.make_listbox.bind('<<ListboxSelect>>', self.load_models)

        self.model_listbox = Tkinter.Listbox(self.main_frame)
        self.model_listbox.pack(fill='both', expand=True, side=Tkinter.LEFT)

        # insert our items into the list box
        for i, item in enumerate(self.data.keys()):
            self.make_listbox.insert(i, item)

    def load_models(self, *args):
        selection = self.make_listbox.selection_get()

        # clear the model listbox
        self.model_listbox.delete(0, Tkinter.END)

        # insert the models into the model listbox
        for i, item in enumerate(self.data[selection]):
            self.model_listbox.insert(i, item)

root = Tkinter.Tk()
app = Application(root)
app.mainloop()