问题描述
|
我有一个选择框
self.chHead = wx.Choice(self.nbItemPane,-1,choices=[])
我有一个清单
items=[equipment(\'Head\',\'BIG HELMET\',555,5,0),equipment(\'Head\',\'MED HELMET\',\'SMA HELMET\',equipment(\'Shoulders\',\'BIG SHOULDERS\',0)
]
我想发生的是,我的选择框的选择值将从项目列表中得出。因此,在这种情况下,当您选择self.chHead的下拉列表时,您只会看到\'BIG HELMET \',\'MED HELMET \'和\'SMA HELMET \'作为选项
设备定义为
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
解决方法
这取决于您对
equipment
的定义。我假装它是一个带有函数GetSlot()
和GetName()
的类,用于检索前两个字段。您可以从以下项目列表中创建“ 6”列表:
choices = [item.GetName() for item in items if item.GetSlot() == \'Head\']
self.chHead = wx.Choice(self.nbItemPane,-1,choices=choices)
唯一的问题是,您没有一种简单的方法来知道您选择的是items
列表中的哪个项目,特别是如果两个或多个项目可以具有相同的名称时。您可以通过将每个列表项的ѭ9设置为items
列表的相应索引来解决此问题。改为这样做:
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)