如何在wxPython ComboBox中存储字符串以外的对象? [英] How can I store objects other than strings in a wxPython ComboBox?

查看:339
本文介绍了如何在wxPython ComboBox中存储字符串以外的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个顶点对象的列表,每个都有自己的标签和id。如何使用此列表作为wxPython ComboBox的模型,以便当用户选择一个选项时,我可以立即检索顶点ID?

I have a list of Vertex objects, each with their own labels and id's. How can I use this list as a model for a wxPython ComboBox such that when a user selects an option, I can immediately retrieve the Vertex id?

看起来只有ComboBox接受字符串作为模型。我不能创建一个标签到id对的字典,因为有重复的标签。

It appears that ComboBox only accepts strings as a model. I cannot create a dictionary of label to id pairs since there are duplicate labels.

我注意到一个ComboCtrl类,我可以子类创建一个专门的组合框,就像有一个更容易的解决方案。

I noticed a ComboCtrl class which I can subclass to create a specialized combo box, but I feel like there is a easier solution to this.

推荐答案

这个主题在今天早些时候出现在wxPython IRC频道, ListBox。幸运的是,这两个窗口小部件都继承自wx.ItemContainer,因此您可以执行以下操作:

This topic came up on the wxPython IRC channel earlier today, but in regards to the ListBox. Fortunately, both the widgets inherit from wx.ItemContainer, so you can do the following:

for item in ObjList:
    self.myCboBox.append(item.label, item)

然后在事件处理程序中, d做类似的操作:

Then in the event handler, you'd do something like:

itemObject = self.myCboBox.GetClientData(self.myCboBox.GetSelection())
itemID = itemObject.id

这应该可以工作。

这篇关于如何在wxPython ComboBox中存储字符串以外的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆