Creek Barbara
Creek Barbara

Reputation: 647

wxPython : Change ComboBox choices

I'm trying to change the choices of the second wx.ComboBox if the user chooses "Campaign" in the first wx.ComboBox.

The code below is not working, I mean the choices on the second wx.ComboBox remain the same.

I need your help with this one. Thank you,

class Test(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id, "Frame aka Window", style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX, size=(400, 635))


        self.filterValues = ['Campaign','Spent', 'Network Impr.', 'Network Clicks', 'Rev. /1000', 'GA Impr.', 'Revenue', 'eCPC', 'CPC', 'CTR'] 
        self.filterComboColumns = wx.ComboBox(self.panel,choices = self.filterValues, size = (100,-1))
        self.Bind(wx.EVT_COMBOBOX, self.onComboValues, self.filterComboColumns)

        self.filterContainsValues = ['Contains', 'Doesn\'t contain' , '<', '>'] 
        self.filterComboContains = wx.ComboBox(self.panel,choices = self.filterContainsValues, size = (100,-1))
        self.filterComboContains.Enable(False)
        self.Bind(wx.EVT_COMBOBOX, self.onComboContains, self.filterComboContains)

        self.filterInput = wx.TextCtrl(self.panel, wx.ID_ANY, size=(145, 24))
        self.filterInput.Enable(False)
        self.filterInput.SetFont(font)
        self.Bind(wx.EVT_TEXT, self.onComboInput, self.filterInput)

    def onComboValues(self, event): 
      cb = event.GetEventObject() 

      if cb.GetValue() == "Campaign":
        self.filterComboContains.Enable(True)
        self.filterContainsValues = []
        self.filterContainsValues.append('Contains')
        self.filterContainsValues.append('Doesn\'t contain')

Upvotes: 0

Views: 1271

Answers (1)

kbr85
kbr85

Reputation: 1446

You were missing only a call to the Set method. It is not enough to change the list containing the options, you need to tell the wx.ComboBox that the list changed. The Set method is the one that does this.

Code with comments (####):

import wx

class Test(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "Frame aka Window", style= wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX, size=(400, 635))

        self.panel = wx.Panel(self)

        self.filterValues = ['Campaign','Spent', 'Network Impr.', 'Network Clicks', 'Rev. /1000', 'GA Impr.', 'Revenue', 'eCPC', 'CPC', 'CTR'] 
        self.filterComboColumns = wx.ComboBox(self.panel, choices = self.filterValues, size = (100,-1))
        self.Bind(wx.EVT_COMBOBOX, self.onComboValues, self.filterComboColumns)

        self.filterContainsValues = ['Contains', 'Doesn\'t contain' , '<', '>'] 
        self.filterComboContains = wx.ComboBox(self.panel,choices = self.filterContainsValues, size = (100,-1))
        self.filterComboContains.Enable(False)
        #self.Bind(wx.EVT_COMBOBOX, self.onComboContains, self.filterComboContains)

        self.filterInput = wx.TextCtrl(self.panel, wx.ID_ANY, size=(145, 24))
        self.filterInput.Enable(False)
        #self.filterInput.SetFont(font)
        #self.Bind(wx.EVT_TEXT, self.onComboInput, self.filterInput)

        #### Sizer
        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.filterComboColumns)
        self.sizer.Add(self.filterComboContains)
        self.sizer.Add(self.filterInput)
        self.panel.SetSizer(self.sizer)

    def onComboValues(self, event): 
        cb = event.GetEventObject() 

        if cb.GetValue() == "Campaign":
            self.filterComboContains.Enable(True)
            self.filterContainsValues = []
            self.filterContainsValues.append('Contains')
            self.filterContainsValues.append('Doesn\'t contain')
            #### The line below is the line you were missing
            self.filterComboContains.Set(self.filterContainsValues)
            #### This is to avoid showing an empty field when the values change
            self.filterComboContains.SetSelection(0)
        else:
            pass


# Run the program
if __name__ == "__main__":
    app = wx.App()
    frame = Test(None, 1)
    frame.Show()
    app.MainLoop()

Upvotes: 3

Related Questions