5

在 HTML 中,我可以创建如下下拉菜单:

<select name="">
    <option value="">TextNode #1</option>
    <option value="">TextNode #2</option>
<select>

现在我想要 wxPython 中类似的东西。问题是我还没有找到解决方案,因为它只允许我放置文本而不是值。

示例 wxPython(创建下拉菜单):

DropDownList = []
Options = {0:"None",1:"All",2:"WTF?!!"}
For Value, TextNode in Options:
    DropDownList.append( TextNode )

wx.ComboBox(panel,value="Select",choices=DropDownList)

好吧...我如何使用文本节点的附加值?...谢谢!

4

2 回答 2

8

您可以使用 ComboBox 的 Append 方法将附加信息添加到控件中的每个项目。

这是有关该过程的教程:http: //www.blog.pythonlibrary.org/2010/12/16/wxpython-storing-object-in-combobox-or-listbox-widgets/

这是文章中的代码示例:

import wx

########################################################################
class Car:
    """"""

    #----------------------------------------------------------------------
    def __init__(self, id, model, make, year):
        """Constructor"""
        self.id = id
        self.model = model
        self.make = make
        self.year = year       


########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        cars = [Car(0, "Ford", "F-150", "2008"),
                Car(1, "Chevrolet", "Camaro", "2010"),
                Car(2, "Nissan", "370Z", "2005")]

        sampleList = []
        self.cb = wx.ComboBox(panel,
                              size=wx.DefaultSize,
                              choices=sampleList)
        self.widgetMaker(self.cb, cars)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.cb, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def widgetMaker(self, widget, objects):
        """"""
        for obj in objects:
            widget.Append(obj.make, obj)
        widget.Bind(wx.EVT_COMBOBOX, self.onSelect)

    #----------------------------------------------------------------------
    def onSelect(self, event):
        """"""
        print "You selected: " + self.cb.GetStringSelection()
        obj = self.cb.GetClientData(self.cb.GetSelection())
        text = """
        The object's attributes are:
        %s  %s    %s  %s

        """ % (obj.id, obj.make, obj.model, obj.year)
        print text

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

希望有帮助!

于 2013-09-03T13:55:01.520 回答
1

根据我wxWidgets在 c++ 上使用的经验,本机不支持它。

您可以做的是创建一个wxComboBox继承自 wxComboBox 的自定义。您的自定义小部件将负责存储映射,每次调用时进行查找,每次调用SetValue时进行反向查找GetValue

于 2013-09-03T07:24:09.213 回答