2011-05-25 4 views
0

私は選択ボックスwxpython:リストからwxChoiceコンテンツを作成するには?

self.chHead = wx.Choice(self.nbItemPane, -1, choices=[]) 

を持っていると私は私が起こるがしたいどのようなリスト

items=[equipment('Head','BIG HELMET',555,5,5,5,5,5,0,0,0,0,0), 
     equipment('Head','MED HELMET',555,5,5,5,5,5,0,0,0,0,0), 
     equipment('Head','SMA HELMET',555,5,5,5,5,5,0,0,0,0,0), 
     equipment('Shoulders','BIG SHOULDERS',555,5,5,5,5,5,0,0,0,0,0) 
     ] 

を持っているが、私のchoiceboxの選択値は、項目のリストから引き出されることです。したがって、この場合には、あなたは

機器が

class equipment(object): 
    def __init__(self, slot, name, armor, str, int, wis, dex, end, val, tough, power, crit, hit): 
     """ 
     Model of the Equipment Object 
     Contains the followign attributes: 
     """ 

     self.slot = slot 
     self.name = name 
     self.armor = armor 
     self.str = str 
     self.int = int 
     self.wis = wis 
     self.dex = dex 
     self.end = end 
     self.val = val 
     self.tough = tough 
     self.power = power 
     self.crit = crit 
     self.hit = hit 

答えて

0

それをとして定義されてあなただけのオプションとして「BIG HELMET」、「MED HELMET」、および「SMA HELMET」を見ることがself.chHeadのドロップダウンを選択したときあなたの定義はequipmentに依存します。私は最初の2つのフィールドを取得するために、関数GetSlot()GetName()を持つクラスのふりをします。あなたは、このような項目のリストからchoicesリストを作成することができます。

choices = [item.GetName() for item in items if item.GetSlot() == 'Head'] 
self.chHead = wx.Choice(self.nbItemPane, -1, choices=choices) 

その唯一の問題は、あなたが、特に、あなたの選択が参照itemsリストからどの項目を知るための簡単な方法を持っていないということです2つ以上の項目が同じ名前を持つことができる場合。各リスト項目のclientDataitemsリストの対応するインデックスに設定することで、それを解決できます。これを代わりに:

self.chHead = wx.Choice(self.nbItemPane, -1) 
for i in range(len(items)): 
    if items[i].GetSlot() == 'Head': 
     self.chHead.Append(item=item[i].GetName(), clientData=i) 
+0

私は機器のクラス情報を追加しました。 – ccwhite1

+0

.appendは私が必要としていたものです。 Tksくそ – ccwhite1

関連する問題