python:除了getString()之外,使用选择框获取数据

发布于 2024-11-11 10:07:49 字数 1027 浏览 2 评论 0原文

我定义了一个具有多个属性的对象..

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)

当用户从下拉列表中选择鲍勃时,我有一个事件

def EvtChoice(self,event):
    self.Name = event.GetString()

这捕获选择的名称,但如何获取其他属性?我目前正在做的是,

     for each in self.items:
        if self.Name == each.name
          #Get other things here

我的想法是,如果我的列表变得很大,那么整个列表的循环将变得非常低效并且实际上是不必要的,因为用户已经选择了我想要的特定项目。我认为我应该能够做的是获取所选项目的索引,但我不确定如何做到这一点,或者即使这是正确的方法。

I have defined an object that has several attribute..

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

lets say then I have a list of things

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)
            ]

I then populate a choice box with SOME of the items from that list

for each in self.things:
    if each.type == 'car':
       self.choiceCar.Append(item=each.name)

When the user selects Bob from the dropdown I have an event for that

def EvtChoice(self,event):
    self.Name = event.GetString()

This captures the name of the selection, but how do I get the other attributes? What I am currently doing is

     for each in self.items:
        if self.Name == each.name
          #Get other things here

My thought is that if my list grows large then this loop through my entire list will become very inefficient and really unneeded since the user has already selected the specific item I want. What I think I should be able to do is to get the index of the selected item, but im not sure how to do that, or even if that is the correct way to go about it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一花一树开 2024-11-18 10:07:49

将数据或对象与 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())

Associating data or objects with wx.Choice or wx.ComboBox is pretty easy. You can see an example using the latter here:

http://www.blog.pythonlibrary.org/2010/12/16/wxpython-storing-object-in-combobox-or-listbox-widgets/

The basic idea is to pass an empty list to the control's constructor and then iterate over the objects and Append them to the control. So something like this:

for obj in self.things:
    self.choiceCar.Append(obj.name, obj)

Then in the event handler for the widget, you can get the object back by doing this:

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