wxPython - ComboBox & Choice Class

A wx.ComboBox object presents a list of items to select from. It can be configured to be a dropdown list or with permanent display.
The selected item from the list is displayed in a text field, which by default is editable, but can be set to be read-only in the presence of wx.CB_READONLY style parameter.
wxPython API contains a wx.Choice class, whose object is also a dropdown list, which is permanently read-only.
The parameters used by wx.ComboBox class constructor are −
Wx.ComboBox(parent, id, value, pos, size, choices[], style)

The value parameter is the text to be displayed in the text box of combobox. It is populated from the items in choices[] collection.
The following style parameters are defined for wx.ComboBox −
wx.CB_SIMPLE
Combobox with permanently displayed list
wx.CB_DROPDOWN
Combobox with dropdown list
wx.CB_READONLY
Chosen item is not editable
wx.CB_SORT
List is displayed in alphabetical order
The following table shows commonly used methods of wx.ComboBox class −
GetCurrentSelection ()
Returns the item being selected
SetSelection()
Sets the item at the given index as selected
GetString()
Returns the string associated with the item at the given index
SetString()
Changes the text of item at the given index
SetValue()
Sets a string as the text displayed in the edit field of combobox
GetValue()
Returns the contents of the text field of combobox
FindString()
Searches for the given string in the list
GetStringSelection()
Gets the text of the currently selected item
Event binders for events generated by this class are as follows −
wx. COMBOBOX
When item from the list is selected
wx. EVT_TEXT
When combobox text changes
wx. EVT_COMBOBOX_DROPDOWN
When list drops down
wx. EVT_COMBOBOX_CLOSEUP
When list folds up
wx.Choice class constructor prototype is as follows −
wx.Choice(parent, id, pos, size, n, choices[], style)

Parameter ‘n’ stands for number of strings with which the choice list is to be initialized. Like comboBox, the list is populated with items in choices[] collection.
For Choice class, only one style parameter is defined. It is wx.CB_SORT. Only one event binder processes the event emitted by this class. It is wx.EVT_CHOICE.
This example displays the features of wx.ComboBox and wx.Choice. Both objects are put in a vertical box sizer. The lists are populated with items in languages[] List object.
languages = ['C', 'C++', 'Python', 'Java', 'Perl'] 
self.combo = wx.ComboBox(panel,choices = languages) 
self.choice = wx.Choice(panel,choices = languages)
Event binders EVT_COMBOBOX and EVT_CHOICE process corresponding events on them.
self.combo.Bind(wx.EVT_COMBOBOX, self.OnCombo) 
self.choice.Bind(wx.EVT_CHOICE, self.OnChoice)

The following handler functions display the selected item from the list on the label.
def OnCombo(self, event): 
   self.label.SetLabel("selected "+ self.combo.GetValue() +" from Combobox") 
	
def OnChoice(self,event): 
   self.label.SetLabel("selected "+ self.choice.
      GetString( self.choice.GetSelection() ) +" from Choice")
The complete code listing is as follows −
import wx  
class Mywin(wx.Frame): 
   def __init__(self, parent, title): 
      super(Mywin, self).__init__(parent, title = title,size = (300,200)) 
		
      panel = wx.Panel(self) 
      box = wx.BoxSizer(wx.VERTICAL) 
      self.label = wx.StaticText(panel,label = "Your choice:" ,style = wx.ALIGN_CENTRE) 
      box.Add(self.label, 0 , wx.EXPAND |wx.ALIGN_CENTER_HORIZONTAL |wx.ALL, 20) 
      cblbl = wx.StaticText(panel,label = "Combo box",style = wx.ALIGN_CENTRE) 
		
      box.Add(cblbl,0,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5) 
      languages = ['C', 'C++', 'Python', 'Java', 'Perl'] 
      self.combo = wx.ComboBox(panel,choices = languages) 
		
      box.Add(self.combo,1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5) 
      chlbl = wx.StaticText(panel,label = "Choice control",style = wx.ALIGN_CENTRE) 
		
      box.Add(chlbl,0,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5) 
      self.choice = wx.Choice(panel,choices = languages) 
      box.Add(self.choice,1,wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL|wx.ALL,5) 
         
      box.AddStretchSpacer() 
      self.combo.Bind(wx.EVT_COMBOBOX, self.OnCombo) 
      self.choice.Bind(wx.EVT_CHOICE, self.OnChoice)
		
      panel.SetSizer(box) 
      self.Centre() 
      self.Show() 
		  
   def OnCombo(self, event): 
      self.label.SetLabel("You selected"+self.combo.GetValue()+" from Combobox") 
		
   def OnChoice(self,event): 
      self.label.SetLabel("You selected "+ self.choice.GetString
         (self.choice.GetSelection())+" from Choice") 
                             
app = wx.App() 
Mywin(None,  'ComboBox and Choice demo') 
app.MainLoop()
The above code produces the following output −
 Print
 Add Notes
 Bookmark this page