2013-10-30 169 views
1

嗨,所有的样子,并可能能够提供帮助。我对Python很陌生,一般都是编码。我正在研究一个程序,它的基本概念是有几个列表,从一个列表中选择一个,然后根据第一个选择填充另一个列表。我已经能够创建图形用户界面,我已经能够创建各种不同形式的“列表框”,我已经能够遵循教程来展示如何检索“列表框”的标记,但是我没有能够获得第二个列表框来填写我为第一个框中的选择创建的列表。我也尝试过复选框,我在那一个上靠得更近,但仍然没有骰子。从选择填充列表框,python

下面提供的代码的意义在于我可以将第二个列表打印到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() 

回答

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() 
+0

感谢晚五....我已经向前移动,并已经能够看到我需要使用绑定...我看到虽然我的代码相比较,虽然我在那里遇到了问题,可能实际上帮助我解决了现在使用.csv文件和导入到字典中的问题。谢谢你。 –

+0

太好了,很高兴帮助! – Fiver