How to create dropdown with value and text node - WXPython

13,422

Solution 1

You can use the ComboBox's Append method to add additional information to each item in the control.

Here's a tutorial about the process: http://www.blog.pythonlibrary.org/2010/12/16/wxpython-storing-object-in-combobox-or-listbox-widgets/

And here's a code example from the article:

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

Hope that helps!

Solution 2

According to my experience of using wxWidgets on c++, it's not supported natively.

What you could do is create a custom wxComboBox which would inherit from wxComboBox. Your custom widget will be responsible for storing the mapping, do a lookup everytime you call SetValue and do a reverse look up everytime you call GetValue

Share:
13,422
Jorge Olaf
Author by

Jorge Olaf

:D

Updated on June 04, 2022

Comments

  • Jorge Olaf
    Jorge Olaf almost 2 years

    In HTML I can create drop-down menus like this:

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

    Now I want something similar in wxPython. The problem is that I have not found a solution, as it only allows me to place the text and not the value.

    Example wxPython( Create Dropdown ):

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

    Well... How I can use a value addition to the text node?.. Thanks!