2011-06-02 70 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