开发者

How can I store objects other than strings in a wxPython ComboBox?

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?

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.

I not开发者_如何学Pythoniced a ComboCtrl class which I can subclass to create a specialized combo box, but I feel like there is a easier solution to this.


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)

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

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

That should work.


Most straightforward approach would be storing vertexes in a list and retrieve selected value by index (returned by wx.ComboBox GetSelection()).

Edit: q&d example:

l = [{"value" : value_1, "label" : "label"},
     {"value" : value_2, "label" : "label"}]

def on_select (event):
    i = event.GetSelection()
    print (l[i]["value"])

# ui construction omitted    

Bind (wx.EVT_COMBOBOX, on_slect)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜