wxPython ToggleButton Widget

In this wxPython Tutorial, we will demonstrate how to use the ToggleButton Widget, alongside it’s various styles, features and functions. A complete list of options will be included here, alongside several code examples for your convenience.

wxPython ToggleButton Syntax

The syntax structure required to create a ToggleButton Widget in wxPython.

ToggleButton(parent, id, label, pos,
 size, style, val, name)
  1. parent: This is the Widget to which it is parented, such as a Panel.
  2. id: Widget ID. Default value is ID_ANY, which gives it the next available ID.
  3. label: The Text which you want to display on the Widget.
  4. pos: A tuple containing the coordinates of where the topleft corner of the StaticText Widget should begin from.
  5. size: A tuple which defines the dimensions of the area occupied by the Text.
  6. style: Used for styling the Button.

wxPython ToggleButton Methods

A list of useful methods for the ToggleButton Widget.

GetValue()Returns a bool (True/False) to reflect the current state of the ToggleButton.
SetValue(bool)Sets the ToggleButton to the state mentioned in the parameters.
SetLabel(string)Sets the label on the ToggleButton to the given String.
GetString()Returns the string value of the Label on the ToggleButton.

Example Code

A simple example where we create two ToggleButton Widgets, and set one of them to the On state.

import wx

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

        toggle1 = wx.ToggleButton(self.panel, label ="Off", pos = (50, 50))
        toggle2 = wx.ToggleButton(self.panel, label ="On", pos = (150, 50))



app = wx.App()
window = Window("WxPython Tutorial")

The output, where you can see both the two ToggleButton’s in different states.

wxPython Toggle Button Widget Example

ToggleButton Events and Function Example

In this example we’ll take a look at how to Bind a ToggleButton to a function that triggers when it is pressed.

The Code below binds a ToggleButton to the ToggleButtonEvent() function. This function changes the text of the toggleButton, depending on what it’s current value is. In short, clicking it while it’s off, will change the text to “On”.

Run the code for yourself to try it out.

import wx

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

        toggle = wx.ToggleButton(self.panel, label ="Off", pos = (50, 50))
        toggle.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleButtonEvent)


    def ToggleButtonEvent(self, e):
        toggleButton = e.GetEventObject()
        if (toggleButton.GetValue() == True):
            print("turning on")
            print("turning off")

app = wx.App()
window = Window("WxPython Tutorial")

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

Notify of
Inline Feedbacks
View all comments