wxPython Events and Event Handling

In this tutorial we will explore wxPython Events, and how they are handled.

Events play a very important role in just about every GUI framework. This is because GUI applications, such as wxPython are Event driven. They are continuously detecting which events are currently occurring, and then act accordingly.

This is also the reason behind the MainLoop() in our wxPython code, which runs an infinite loop, detecting events in every iteration.


What are Events?

An “event” is basically an action or a trigger that occurs when a specific situation arises. For example, if the text inside a TextCtrl widget is modified, this will trigger the “Text has been Changed” event. Other common examples of Events are with input devices like the Mouse and Keyboard.

An “Event Object”, is the widget/object associated with the event. Meaning, the widget/object that triggered the event (usually). You can typically use the GetEventObject() method on the event to return the Event Object.


Binding Events in wxPython

The most important thing when it comes to Event Handling in wxPython, is the binding of events to widgets.

Whenever you create a Widget that you want the user to interact with, you need to Bind it to an event and a function. Why the function? Because how it works, is that when widget triggers the event, and this event is detected, then that function will be automatically called.

To bind events to a widget, we use the “Bind” function. This is used on the widget that we want to bind, and take two parameters. The first parameter is the event to which you are binding it. Each widget has it’s own events, so this will vary from widget to widget.

The second parameter is the function that you want to bind the widget to. This function is called automatically when the event is detected.

Below is a simple example showing how Binding is done on a widget.

button.Bind(wx.EVT_BUTTON, self.Quit)

Example Code

Below is a simple example of binding an Event to a Widget. Every widget has it’s own Event(s), and there are many others triggered by actions such as closing the window, pressing a key etc.

In our example we want to bind a Button to a function that closes the Window. Here’s the general process:

  1. Create the Button Widget
  2. Create the function that you called when the Button is Clicked.
  3. Use the Bind() method with the wx.EVT_BUTTON event and the name of the function you want called.
  4. When the button is clicked, the wx.EVT_BUTTON event is triggered. This event is then propagated, until it is handled by the widget it was bound to.
  5. As the wx.EVT_BUTTON event on the Button widget was bound to the onClick() function, it is called and executed.
import wx

class Window(wx.Frame):
    def __init__(self, title):
        super().__init__(parent = None, title = title)
        self.panel = wx.Panel(self)

        button = wx.Button(self.panel, label = "Press Me",
                           pos = (50, 50))
        button.Bind(wx.EVT_BUTTON, self.onClick)

        self.Centre()
        self.Show()

    def onClick(self, e):
        self.Close()
        
app = wx.App()
window = Window("WxPython Tutorial")
app.MainLoop()

Alternatively, we can handle the Binding in a slightly different manner.

There is another version (overload) of the Bind() method, which you can use on a parent of the widget. For example, the Button is child of the panel, and a grand-child of the frame.

Hence this,

self.Bind(wx.EVT_BUTTON, self.onClick, button)

and this,

self.panel.Bind(wx.EVT_BUTTON, self.onClick, button)

will work in exactly the same way as calling the Bind() method on the widget.


Unbinding Events in wxPython

Just like how we can bind Events, we can also Unbind them using the Unbind() method, which has the opposite effect of Bind().

The below code will create a Button which you can click 5 times, before it gets Unbinded from the event which causes the function to be triggered. You can also choose to re-bind it to some other function.

import wx

class Window(wx.Frame):
    def __init__(self, title):
        super().__init__(parent = None, title = title)
        self.panel = wx.Panel(self)
        self.count = 0

        self.button = wx.Button(self.panel, label = "Press Me",
                                pos = (50, 50))
        self.Bind(wx.EVT_BUTTON, self.onClick, self.button)

        self.Centre()
        self.Show()

    def onClick(self, e):
        self.count += 1
        print("value: ", self.count)

        if self.count == 5:
            self.Unbind(wx.EVT_BUTTON, self.button)
        
app = wx.App()
window = Window("WxPython Tutorial")
app.MainLoop()

List of Events

Here we have a complete list of events available in wxPython. (271 in total).

We have used the following code to locate all events in wxPython’s latest version (Phoenix) and print them out here. Should you run this code yourself, all events in your current wxPython installation will be printed out.

import wx

for x in dir(wx):
  if x.startswith('EVT_'):
      print(x)
Full Event List
  • EVT_ACTIVATE
  • EVT_ACTIVATE_APP
  • EVT_BOOKCTRL_PAGE_CHANGED
  • EVT_BOOKCTRL_PAGE_CHANGING
  • EVT_BUTTON
  • EVT_CATEGORY_ALL
  • EVT_CATEGORY_SOCKET
  • EVT_CATEGORY_THREAD
  • EVT_CATEGORY_TIMER
  • EVT_CATEGORY_UI
  • EVT_CATEGORY_USER_INPUT
  • EVT_CHAR
  • EVT_CHAR_HOOK
  • EVT_CHECKBOX
  • EVT_CHECKLISTBOX
  • EVT_CHILD_FOCUS
  • EVT_CHOICE
  • EVT_CHOICEBOOK_PAGE_CHANGED
  • EVT_CHOICEBOOK_PAGE_CHANGING
  • EVT_CLIPBOARD_CHANGED
  • EVT_CLOSE
  • EVT_COLLAPSIBLEHEADER_CHANGED
  • EVT_COLLAPSIBLEPANE_CHANGED
  • EVT_COLOURPICKER_CHANGED
  • EVT_COLOURPICKER_CURRENT_CHANGED
  • EVT_COLOURPICKER_DIALOG_CANCELLED
  • EVT_COLOUR_CHANGED
  • EVT_COMBOBOX
  • EVT_COMBOBOX_CLOSEUP
  • EVT_COMBOBOX_DROPDOWN
  • EVT_COMMAND_ENTER
  • EVT_COMMAND_KILL_FOCUS
  • EVT_COMMAND_LEFT_CLICK
  • EVT_COMMAND_LEFT_DCLICK
  • EVT_COMMAND_RIGHT_CLICK
  • EVT_COMMAND_RIGHT_DCLICK
  • EVT_COMMAND_SCROLL
  • EVT_COMMAND_SCROLL_BOTTOM
  • EVT_COMMAND_SCROLL_CHANGED
  • EVT_COMMAND_SCROLL_ENDSCROLL
  • EVT_COMMAND_SCROLL_LINEDOWN
  • EVT_COMMAND_SCROLL_LINEUP
  • EVT_COMMAND_SCROLL_PAGEDOWN
  • EVT_COMMAND_SCROLL_PAGEUP
  • EVT_COMMAND_SCROLL_THUMBRELEASE
  • EVT_COMMAND_SCROLL_THUMBTRACK
  • EVT_COMMAND_SCROLL_TOP
  • EVT_COMMAND_SET_FOCUS
  • EVT_CONTEXT_MENU
  • EVT_DETAILED_HELP
  • EVT_DETAILED_HELP_RANGE
  • EVT_DIRCTRL_FILEACTIVATED
  • EVT_DIRCTRL_SELECTIONCHANGED
  • EVT_DIRPICKER_CHANGED
  • EVT_DISPLAY_CHANGED
  • EVT_DPI_CHANGED
  • EVT_DROP_FILES
  • EVT_END_PROCESS
  • EVT_END_SESSION
  • EVT_ENTER_WINDOW
  • EVT_ERASE_BACKGROUND
  • EVT_FILECTRL_FILEACTIVATED
  • EVT_FILECTRL_FILTERCHANGED
  • EVT_FILECTRL_FOLDERCHANGED
  • EVT_FILECTRL_SELECTIONCHANGED
  • EVT_FILEPICKER_CHANGED
  • EVT_FIND
  • EVT_FIND_CLOSE
  • EVT_FIND_NEXT
  • EVT_FIND_REPLACE
  • EVT_FIND_REPLACE_ALL
  • EVT_FONTPICKER_CHANGED
  • EVT_FSWATCHER
  • EVT_GESTURE_PAN
  • EVT_GESTURE_ROTATE
  • EVT_GESTURE_ZOOM
  • EVT_HEADER_BEGIN_REORDER
  • EVT_HEADER_BEGIN_RESIZE
  • EVT_HEADER_CLICK
  • EVT_HEADER_DCLICK
  • EVT_HEADER_DRAGGING_CANCELLED
  • EVT_HEADER_END_REORDER
  • EVT_HEADER_END_RESIZE
  • EVT_HEADER_MIDDLE_CLICK
  • EVT_HEADER_MIDDLE_DCLICK
  • EVT_HEADER_RESIZING
  • EVT_HEADER_RIGHT_CLICK
  • EVT_HEADER_RIGHT_DCLICK
  • EVT_HEADER_SEPARATOR_DCLICK
  • EVT_HELP
  • EVT_HELP_RANGE
  • EVT_HIBERNATE
  • EVT_HOTKEY
  • EVT_ICONIZE
  • EVT_IDLE
  • EVT_INIT_DIALOG
  • EVT_JOYSTICK_EVENTS
  • EVT_JOY_BUTTON_DOWN
  • EVT_JOY_BUTTON_UP
  • EVT_JOY_MOVE
  • EVT_JOY_ZMOVE
  • EVT_KEY_DOWN
  • EVT_KEY_UP
  • EVT_KILL_FOCUS
  • EVT_LEAVE_WINDOW
  • EVT_LEFT_DCLICK
  • EVT_LEFT_DOWN
  • EVT_LEFT_UP
  • EVT_LISTBOOK_PAGE_CHANGED
  • EVT_LISTBOOK_PAGE_CHANGING
  • EVT_LISTBOX
  • EVT_LISTBOX_DCLICK
  • EVT_LIST_BEGIN_DRAG
  • EVT_LIST_BEGIN_LABEL_EDIT
  • EVT_LIST_BEGIN_RDRAG
  • EVT_LIST_CACHE_HINT
  • EVT_LIST_COL_BEGIN_DRAG
  • EVT_LIST_COL_CLICK
  • EVT_LIST_COL_DRAGGING
  • EVT_LIST_COL_END_DRAG
  • EVT_LIST_COL_RIGHT_CLICK
  • EVT_LIST_DELETE_ALL_ITEMS
  • EVT_LIST_DELETE_ITEM
  • EVT_LIST_END_LABEL_EDIT
  • EVT_LIST_INSERT_ITEM
  • EVT_LIST_ITEM_ACTIVATED
  • EVT_LIST_ITEM_CHECKED
  • EVT_LIST_ITEM_DESELECTED
  • EVT_LIST_ITEM_FOCUSED
  • EVT_LIST_ITEM_MIDDLE_CLICK
  • EVT_LIST_ITEM_RIGHT_CLICK
  • EVT_LIST_ITEM_SELECTED
  • EVT_LIST_ITEM_UNCHECKED
  • EVT_LIST_KEY_DOWN
  • EVT_LONG_PRESS
  • EVT_MAGNIFY
  • EVT_MAXIMIZE
  • EVT_MENU
  • EVT_MENU_CLOSE
  • EVT_MENU_HIGHLIGHT
  • EVT_MENU_HIGHLIGHT_ALL
  • EVT_MENU_OPEN
  • EVT_MENU_RANGE
  • EVT_MIDDLE_DCLICK
  • EVT_MIDDLE_DOWN
  • EVT_MIDDLE_UP
  • EVT_MOTION
  • EVT_MOUSEWHEEL
  • EVT_MOUSE_AUX1_DCLICK
  • EVT_MOUSE_AUX1_DOWN
  • EVT_MOUSE_AUX1_UP
  • EVT_MOUSE_AUX2_DCLICK
  • EVT_MOUSE_AUX2_DOWN
  • EVT_MOUSE_AUX2_UP
  • EVT_MOUSE_CAPTURE_CHANGED
  • EVT_MOUSE_CAPTURE_LOST
  • EVT_MOUSE_EVENTS
  • EVT_MOVE
  • EVT_MOVE_END
  • EVT_MOVE_START
  • EVT_MOVING
  • EVT_NAVIGATION_KEY
  • EVT_NC_PAINT
  • EVT_NOTEBOOK_PAGE_CHANGED
  • EVT_NOTEBOOK_PAGE_CHANGING
  • EVT_PAINT
  • EVT_PALETTE_CHANGED
  • EVT_POWER_RESUME
  • EVT_POWER_SUSPENDED
  • EVT_POWER_SUSPENDING
  • EVT_POWER_SUSPEND_CANCEL
  • EVT_PRESS_AND_TAP
  • EVT_QUERY_END_SESSION
  • EVT_QUERY_NEW_PALETTE
  • EVT_RADIOBOX
  • EVT_RADIOBUTTON
  • EVT_RIGHT_DCLICK
  • EVT_RIGHT_DOWN
  • EVT_RIGHT_UP
  • EVT_SCROLL
  • EVT_SCROLLBAR
  • EVT_SCROLLWIN
  • EVT_SCROLLWIN_BOTTOM
  • EVT_SCROLLWIN_LINEDOWN
  • EVT_SCROLLWIN_LINEUP
  • EVT_SCROLLWIN_PAGEDOWN
  • EVT_SCROLLWIN_PAGEUP
  • EVT_SCROLLWIN_THUMBRELEASE
  • EVT_SCROLLWIN_THUMBTRACK
  • EVT_SCROLLWIN_TOP
  • EVT_SCROLL_BOTTOM
  • EVT_SCROLL_CHANGED
  • EVT_SCROLL_ENDSCROLL
  • EVT_SCROLL_LINEDOWN
  • EVT_SCROLL_LINEUP
  • EVT_SCROLL_PAGEDOWN
  • EVT_SCROLL_PAGEUP
  • EVT_SCROLL_THUMBRELEASE
  • EVT_SCROLL_THUMBTRACK
  • EVT_SCROLL_TOP
  • EVT_SEARCH
  • EVT_SEARCHCTRL_CANCEL_BTN
  • EVT_SEARCHCTRL_SEARCH_BTN
  • EVT_SEARCH_CANCEL
  • EVT_SET_CURSOR
  • EVT_SET_FOCUS
  • EVT_SHOW
  • EVT_SIZE
  • EVT_SIZING
  • EVT_SLIDER
  • EVT_SPIN
  • EVT_SPINCTRL
  • EVT_SPINCTRLDOUBLE
  • EVT_SPIN_DOWN
  • EVT_SPIN_UP
  • EVT_SPLITTER_DCLICK
  • EVT_SPLITTER_DOUBLECLICKED
  • EVT_SPLITTER_SASH_POS_CHANGED
  • EVT_SPLITTER_SASH_POS_CHANGING
  • EVT_SPLITTER_UNSPLIT
  • EVT_SYS_COLOUR_CHANGED
  • EVT_TEXT
  • EVT_TEXT_COPY
  • EVT_TEXT_CUT
  • EVT_TEXT_ENTER
  • EVT_TEXT_MAXLEN
  • EVT_TEXT_PASTE
  • EVT_TEXT_URL
  • EVT_THREAD
  • EVT_TIMER
  • EVT_TOGGLEBUTTON
  • EVT_TOOL
  • EVT_TOOLBOOK_PAGE_CHANGED
  • EVT_TOOLBOOK_PAGE_CHANGING
  • EVT_TOOL_DROPDOWN
  • EVT_TOOL_ENTER
  • EVT_TOOL_RANGE
  • EVT_TOOL_RCLICKED
  • EVT_TOOL_RCLICKED_RANGE
  • EVT_TREEBOOK_NODE_COLLAPSED
  • EVT_TREEBOOK_NODE_EXPANDED
  • EVT_TREEBOOK_PAGE_CHANGED
  • EVT_TREEBOOK_PAGE_CHANGING
  • EVT_TREE_BEGIN_DRAG
  • EVT_TREE_BEGIN_LABEL_EDIT
  • EVT_TREE_BEGIN_RDRAG
  • EVT_TREE_DELETE_ITEM
  • EVT_TREE_END_DRAG
  • EVT_TREE_END_LABEL_EDIT
  • EVT_TREE_GET_INFO
  • EVT_TREE_ITEM_ACTIVATED
  • EVT_TREE_ITEM_COLLAPSED
  • EVT_TREE_ITEM_COLLAPSING
  • EVT_TREE_ITEM_EXPANDED
  • EVT_TREE_ITEM_EXPANDING
  • EVT_TREE_ITEM_GETTOOLTIP
  • EVT_TREE_ITEM_MENU
  • EVT_TREE_ITEM_MIDDLE_CLICK
  • EVT_TREE_ITEM_RIGHT_CLICK
  • EVT_TREE_KEY_DOWN
  • EVT_TREE_SEL_CHANGED
  • EVT_TREE_SEL_CHANGING
  • EVT_TREE_SET_INFO
  • EVT_TREE_STATE_IMAGE_CLICK
  • EVT_TWO_FINGER_TAP
  • EVT_UPDATE_UI
  • EVT_UPDATE_UI_RANGE
  • EVT_VLBOX
  • EVT_WINDOW_CREATE
  • EVT_WINDOW_DESTROY
  • EVT_WINDOW_MODAL_DIALOG_CLOSED

This marks the end of the wxPython Events and Event Handling Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments