wxpython:如何从列表创建wxChoice内容?

发布于 2024-11-09 17:04:21 字数 1107 浏览 2 评论 0原文

我有一个选择框

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2024-11-16 17:04:21

这取决于您对设备的定义。我假设它是一个带有函数 GetSlot()GetName() 的类,用于检索前两个字段。您可以从项目列表中创建您的 choices 列表,如下所示:

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

唯一的问题是您没有简单的方法来知道 items 中的哪个项目列出您的选择所指的内容,特别是当两个或多个项目可以具有相同名称时。您可以通过将每个列表项的 clientData 设置为 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)

It depends on your definition of equipment. I'll pretend it's a class with functions GetSlot() and GetName() for retrieving the first two fields. You could create your choices list from the list of items like this:

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

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 the clientData for each list item to the corresponding index of the items list. Do this instead:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文