Search code examples
pythoncomboboxwindowwxpython

wxpython combobox value is not changed once selected


I have wxpython code (wxpython version: 4.2.0). It has two Comboboxes to select value of x and y, and list of values for y is determined by x.

self.x = wx.ComboBox( self, wx.ID_ANY, "x", wx.Point( 60, 43 ), (220, 30), x_choices, 0 )
self.x.Bind(wx.EVT_COMBOBOX, self.update_y)

def update_y(self, event):
    y_choice = df.loc[df['A'] == self.x.GetStringSelection(), 'y'].tolist()
    self.y = wx.ComboBox( self, wx.ID_ANY, "y", (60, 73), (220, 30), y_choice, 0 )

It works on Macbook, but do not work on Windows 10. To be more concrete, once value of x is selected, then the value is not changed anymore. How to solve the problem and why this problem occurs?


Solution

  • The code creates the self.y combo box on every event, and you are ending up with comboboxes stacked on top of each other. By assigning a new combo box to self.y the existing one does not get magically deallocated from the wxwidgets/C++ part.

    The right way to do is to create the combo once and then modify its items during runtime.

    # create the control only once, populate with empty list
    self.y = wx.ComboBox( self, wx.ID_ANY, "y", (60, 73), (220, 30), [], 0 )
    
    self.x = wx.ComboBox( self, wx.ID_ANY, "x", wx.Point( 60, 43 ), (220, 30), x_choices, 0 )
    self.x.Bind(wx.EVT_COMBOBOX, self.update_y)
    
    def update_y(self, event):
        y_choice = df.loc[df['A'] == self.x.GetStringSelection(), 'y'].tolist()
        # populate choices
        self.y.Set(y_choice)
        # pick a specific choice if needed
        self.y.SetValue(y_choice[0])