2011-06-02 72 views
0

我已经定义了具有多个属性的对象的getString()..蟒蛇:使用选择框来获取数据,除了

class thing(object): 
    def __init__(self, type, name, attrA, attrB, attrC): 
     self.type = type 
     self.name = name 
     self.attrA = attrA 
     self.attrB = attrB 
     self.attrC = attrC 

可以说,那么我的事情的清单

self.things=[thing('car','fred',1,2,3), 
      thing('car','george',a,b,c), 
      thing('truck','bob',6,7,8), 
      thing('truck','tom',x,y,z) 
      ] 

然后,我填充选择框中的某些项目从该列表

for each in self.things: 
    if each.type == 'car': 
     self.choiceCar.Append(item=each.name) 

当用户从下拉列表中选择鲍勃我有一个n事件

def EvtChoice(self,event): 
    self.Name = event.GetString() 

这将捕获选择的名称,但是如何获取其他属性?什么我目前做的是

 for each in self.items: 
     if self.Name == each.name 
      #Get other things here 

我的想法是,如果我的列表变大则因为用户已经选择了我想要的特定项目这个循环通过我的整个列表会变得非常低效的,真正不必要的。我认为我应该能够做的就是获得所选项目的索引,但我不知道该怎么做,或者即使这是正确的方式去做。

回答

2

将数据或对象与wx.Choice或wx.ComboBox关联起来非常简单。你可以在这里使用后者看一个例子:

http://www.blog.pythonlibrary.org/2010/12/16/wxpython-storing-object-in-combobox-or-listbox-widgets/

的基本思想是通过一个空列表控件的构造函数,然后遍历对象,并把它们添加到控制。因此,像这样:

for obj in self.things: 
    self.choiceCar.Append(obj.name, obj) 

然后在该插件的事件处理程序,你可以这样做,得到的对象返回:

obj = self.choiceCar.GetClientData(self.choiceCar.GetSelection()) 
+0

正是我需要的。谢谢 – ccwhite1 2011-06-02 14:31:07