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)
]
我想要发生的是我的选择框的选择值将从项目列表中提取。因此,在这种情况下,当您选择 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
I have a choice box
self.chHead = wx.Choice(self.nbItemPane, -1, choices=[])
And I have a list
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)
]
What I want to have happen is that the choice values of my choicebox will be drawn from the list of items. So in this case when you selected the dropdown of self.chHead you would only see 'BIG HELMET', 'MED HELMET', and 'SMA HELMET' as options
Equipment is defined as
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您对
设备
的定义。我假设它是一个带有函数GetSlot()
和GetName()
的类,用于检索前两个字段。您可以从项目列表中创建您的choices
列表,如下所示:唯一的问题是您没有简单的方法来知道
items
中的哪个项目列出您的选择所指的内容,特别是当两个或多个项目可以具有相同名称时。您可以通过将每个列表项的clientData
设置为items
列表的相应索引来解决此问题。改为这样做:It depends on your definition of
equipment
. I'll pretend it's a class with functionsGetSlot()
andGetName()
for retrieving the first two fields. You could create yourchoices
list from the list of items like this:The only problem with that is that you don't have an easy way to know which item from the
items
list your selection refers to, especially if two or more items can have the same name. You can solve that by setting theclientData
for each list item to the corresponding index of theitems
list. Do this instead: