Dynamically change the choices in a wx.ComboBox()

25,309

wx.ComboBox derives from wx.ItemContainer, which has methods for Appending, Clearing, Inserting and Deleting items, all of these methods are available on wx.ComboBox.

One way to do what you want would be to define the text_return() method as follows:

def text_return(self, event):
    self.st.Clear()
    self.st.Append('3')
    self.st.Append('4')
Share:
25,309
Orjanp
Author by

Orjanp

Updated on July 14, 2020

Comments

  • Orjanp
    Orjanp almost 4 years

    I didn't find a better way to change the different choices in a wx.ComboBox() than swap the old ComboBox with a new one. Is there a better way?

    Oerjan Pettersen

    #!/usr/bin/python
    
    #20_combobox.py
    
    import wx
    import wx.lib.inspection
    
    class MyFrame(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, *args, **kwargs)
    
            self.p1 = wx.Panel(self)
            lst = ['1','2','3']
            self.st = wx.ComboBox(self.p1, -1, choices = lst, style=wx.TE_PROCESS_ENTER)
    
            self.st.Bind(wx.EVT_COMBOBOX, self.text_return)
    
    
        def text_return(self, event):
            lst = ['3','4']
            self.st = wx.ComboBox(self.p1, -1, choices = lst, style=wx.TE_PROCESS_ENTER)
    
    
    class MyApp(wx.App):
        def OnInit(self):
            frame = MyFrame(None, -1, '20_combobox.py')
            frame.Show()
            self.SetTopWindow(frame)
            return 1
    
    if __name__ == "__main__":
        app = MyApp(0)
    #    wx.lib.inspection.InspectionTool().Show()
        app.MainLoop()
    
  • Toni Ruža
    Toni Ruža about 15 years
    or self.sf.AppendItems(['3', '4'])
  • Moe
    Moe about 15 years
    @Toni Cool I was looking for that - but for some reason that function isn't documented.
  • Orjanp
    Orjanp about 15 years
    Thanks. AppendItems is in the new docs, but not in the old ones.
  • Tong
    Tong over 9 years
    Docs could be reached at wxpython.org/Phoenix/docs/html/…
  • Magix
    Magix almost 8 years
    Update : doc links are down
  • RobinDunn
    RobinDunn almost 8 years
    The internal structure of the docs has changed, so the document at that link no longer exists. You can still get to the docs for that method by drilling down from the root page: wxpython.org/Phoenix/docs/html/main.html