Python GUI Creating ListBox in wxPython

In this Python GUI article i want to show Creating ListBox in wxPython. so this will be a simple project that a user can add, delete and edit a ListBox.

 

 

What is wxPython ListBox?

Listbox is used to select one or more of a list of strings. so The strings are displayed in a scrolling box, with the selected string(s) marked in reverse video. a listbox can be single selection (if an item is selected, the previous selection is removed) or multiple selection (clicking an item toggles the item on or off independently of other selections).

 

This class supports the following styles:

  • wx.LB_SINGLE:  So it is a Single-selection list.
  • wx.LB_MULTIPLE: Multiple-selection list: the user can toggle multiple items on and off. This is the same as wx.LB_EXTENDED in wxGTK2 port.
  • wx.LB_EXTENDED: Extended-selection list: the user can extend the selection by using SHIFT or CTRL keys together with the cursor movement keys or the mouse.
  • wx.LB_HSCROLL: Create horizontal scrollbar if contents are too wide (Windows only).
  • wx.LB_ALWAYS_SB: Always show a vertical scrollbar.
  • wx.LB_NEEDED_SB: Only create a vertical scrollbar if needed.
  • wx.LB_NO_SB: Don’t create vertical scrollbar (wxMSW and wxGTK only).
  • wx.LB_SORT: The listbox contents are sorted in alphabetical order.

 

 

 

 

Python GUI Creating ListBox in wxPython

Let’s create our example, So this is the complete code for Python GUI Creating ListBox in wxPython.

 

 

 

 

 

So these are the methods that we have used for binding

Now let’s describe this code:

 

  • Importing wxPython Module:

This line imports the wxPython module, which is a Python wrapper for the wxWidgets C++ library. wxPython provides a set of tools for creating graphical user interfaces (GUIs) in Python.

 

 

  • Defining MyFrame Class:

This class defines the main frame of the application. It inherits from wx.Frame, which is the main window class in wxPython. __init__ method initializes the frame with a specific title and size. It also sets the frame icon using the SetIcon() method with a PNG file, After that it creates an instance of the MyPanel class to serve as the main panel within the frame.

 

 

 

  • Defining MyPanel Class:

This class defines the main panel of the application. It inherits from wx.Panel. __init__ method initializes the panel with a horizontal box sizer (hbox). Inside the horizontal box sizer, it adds a list box widget (self.listbox) with proportion 1 to make it expandable. It also creates a panel (btnPanel) for buttons and adds four buttons (‘New’, ‘Rename’, ‘Delete’, ‘Clear’) to it vertically using a vertical box sizer (vbox). event handlers for button clicks and list box double-clicks are bound to their methods. And lastly it sets the main panel’s sizer (hbox) and centers the panel inside its parent window.

 

 

 

  • Defining MyApp Class:

This class defines the application object. It inherits from wx.App. The OnInit method is called when the application starts. It creates an instance of the MyFrame class as the main application window, sets its title, and shows it.

 

 

  • Running Application:

This creates an instance of the MyApp class and starts the wxPython event loop, allowing the application to respond to user input and events.

 

 

 

 

Run the code and this will be the result

Python GUI Creating ListBox in wxPython
Python GUI Creating ListBox in wxPython

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×