Python – Dynamically change the choices in a wx.ComboBox()


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


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'] = wx.ComboBox(self.p1, -1, choices = lst, style=wx.TE_PROCESS_ENTER), self.text_return)

    def text_return(self, event):
        lst = ['3','4'] = wx.ComboBox(self.p1, -1, choices = lst, style=wx.TE_PROCESS_ENTER)

class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, '')
        return 1

if __name__ == "__main__":
    app = MyApp(0)
#    wx.lib.inspection.InspectionTool().Show()

Best Solution

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):'3')'4')