Python GUI ComboBox in wxPython

In this Python GUI tutorial i want to show you creating of ComboBox in wxPython. first of all let’s talk about Python ComboBox in xPython.

 

 

What is wxPython ComboBox?

A combobox is like a combination of an edit control and a listbox. So you can display ComboBox as static list or read-only text field, a ComboBox is a single selection only.

 

This class supports the following styles:

  • wx.CB_SIMPLE: Creates a combobox with a permanently displayed list. Windows only.
  • wx.CB_DROPDOWN: Creates a combobox with a drop-down list. MSW and Motif only.
  • wx.CB_READONLY: A combobox with this style behaves like a wx.Choice (and may look in the same way as well, although this is platform-dependent), i.e. it allows the user to choose from the list of options but doesn’t allow to enter a value not present in the list.
  • wx.CB_SORT: Sorts the entries in the list alphabetically. Notice that this style is not currently implemented in wxOSX.
  • wx.TE_PROCESS_ENTER: The control will generate the event wxEVT_TEXT_ENTER (otherwise pressing Enter key is either processed internally by the control or used for navigation between dialog controls).

 

 

Handlers bound for the following event types will receive a wx.CommandEvent parameter.

  • EVT_COMBOBOX: Process a wxEVT_COMBOBOX event, when an item on the list is selected. Note that calling GetValue returns the new value of selection.
  • EVT_TEXT: Process a wxEVT_TEXT event, when the combobox text changes.
  • EVT_TEXT_ENTER: Process a wxEVT_TEXT_ENTER event, when RETURN is pressed in the combobox (notice that the combobox must have been created with wx.TE_PROCESS_ENTER style to receive this event).
  • EVT_COMBOBOX_DROPDOWN: Process a wxEVT_COMBOBOX_DROPDOWN event, which is generated when the list box part of the combo box is shown (drops down). Notice that this event is only supported by wxMSW, wxGTK with GTK+ 2.10 or later, and OSX/Cocoa.
  • EVT_COMBOBOX_CLOSEUP: Process a wxEVT_COMBOBOX_CLOSEUP event, which is generated when the list box of the combo box disappears (closes up). This event is only generated for the same platforms as wxEVT_COMBOBOX_DROPDOWN above

 

 

 

Python GUI ComboBox in wxPython

Now let’s create our example, This is the complete code for Python GUI ComboBox in wxPython.

 

 

 

This is class is MyFrame class that inherits from wx.Frame and it is a top level window that contains our panel.

 

 

 

After that we have MyPanel class that inherits from wx.Panel and it is the place that we create our widgets and layouts in this class. you can see that we have created our ComboBox in this class .

 

 

 

This is the label that we have created

 

 

And this is the ComboBox creation in wxPython

 

 

 

In this line of code we have added our ComboBox items with a method

 

 

 

So this is the method for our Combobox

 

 

 

So the last class is MyApp class that inherits from wx.App. the OnInit() method is where you will most often create frame subclass objects. and start our main loop.That’s it. Once the application’s main event loop processing takes over, control passes to wxPython. Unlike procedural programs, a wxPython GUI program primarily responds to the events taking place around it, mostly determined by a  human user clicking with a mouse and typing at the keyboard. When all the frames in an application have been closed, the app.MainLoop() method will return and the program will exit.

 

 

 

 

So run the code and this is the result

Python GUI ComboBox in wxPython
Python GUI ComboBox in wxPython

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×