Skip to content

Instantly share code, notes, and snippets.

@driscollis
Created January 3, 2020 16:37
Show Gist options
  • Save driscollis/b64ed5594c6e079fe3848dbdb0825420 to your computer and use it in GitHub Desktop.
Save driscollis/b64ed5594c6e079fe3848dbdb0825420 to your computer and use it in GitHub Desktop.
Changing a wx.CombBox contents dynamically
import wx
class MainPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.cb_value = 'One'
self.combo_contents = ['One', 'Two', 'Three']
self.cb = wx.ComboBox(self, choices=self.combo_contents,
value=self.cb_value, size=(100, -1))
self.cb.Bind(wx.EVT_TEXT, self.on_text_change)
self.cb.Bind(wx.EVT_COMBOBOX, self.on_selection)
def on_text_change(self, event):
current_value = self.cb.GetValue()
if current_value != self.cb_value and current_value not in self.combo_contents:
# Value has been edited
index = self.combo_contents.index(self.cb_value)
self.combo_contents.pop(index)
self.combo_contents.insert(index, current_value)
self.cb.SetItems(self.combo_contents)
self.cb.SetValue(current_value)
self.cb_value = current_value
def on_selection(self, event):
self.cb_value = self.cb.GetValue()
class MainFrame(wx.Frame):
def __init__(self):
super().__init__(None, title='ComboBox Changing Demo')
panel = MainPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment