Skip to content

Switch

A Switch is a widget that can be toggled on or off by the user. It consists of a switch button and an optional label.

Examples

Theme Switcher

This example shows how to use a Switch to toggle between light and dark mode in an application.

theme_switch.py
import gooeypie as gp
def toggle_theme(event):
"""Toggles the theme of the application."""
if app.theme == "light":
app.theme = "dark"
else:
app.theme = "light"
app = gp.GooeyPieApp("Theme Switch")
# Create the frame and weather status image and text
weather_frame = gp.Frame()
weather_img = gp.Image("mostly-sunny.png")
weather_lbl = gp.Label("Mostly Sunny")
weather_lbl.style.font_size = 16
weather_frame.add(weather_img, 1, 1)
weather_frame.add(weather_lbl, 2, 1)
# Create switch and set initial position based on current theme
theme_switch = gp.Switch("Dark Mode")
if app.theme == "dark":
theme_switch.value = True
# Add event listener for switch
theme_switch.on_change(toggle_theme)
app.add(weather_frame, 1, 1)
app.add(theme_switch, 1, 2)
app.run()

Creating a Switch widget

my_switch = gp.Switch(label_text)

Parameters

  • label_text (string): The text of the switch.

Properties

Name Type Description
.text string Gets or sets the text of the switch.
.value boolean Gets or sets the value of the switch. Setting this property to True will turn the switch on, while setting it to False will turn it off.
.width integer Gets or sets the width of the switch, including the label.
.height integer Gets or sets the height of the switch, including the label.
.switch_width integer Gets or sets the width of the switch itself, excluding the label.
.switch_height integer Gets or sets the height of the switch itself, excluding the label.
.disabled boolean Gets or sets whether the switch is disabled. When disabled is set to True, the switch does not respond to click events.

Methods

Name Returns Description
.toggle() None Toggles the value of the switch. If the switch is currently on, it will be turned off, and if it is currently off, it will be turned on.

Events

This widget supports the following events as well as the standard events listed below.

Name Description
.on_change(event_function) The function to call when the switch value changes. This event is triggered whenever the user clicks on the switch to change its value, or when the value is changed programmatically.

Standard events

  • .on_click
  • .on_double_click
  • .on_right_click
  • .on_middle_click
  • .on_mouse_down
  • .on_mouse_up
  • .on_mouse_enter
  • .on_mouse_leave

Styles

Styles for this widget

How to specify colors

  • Colors can be specified as either a hex code (e.g. "#FF0000"), a CSS color name, or "transparent".
  • When specifying a color for a style, you can provide either a single color or two colors separated by a comma. If you provide two colors, the first will be used for light mode and the second for dark mode. For example, my_widget.text_color = "steelblue", "skyblue" would specify steelblue for light mode and sky blue for dark mode.
Name Description Default
.style.border_color The color of the border of the switch. "transparent"
.style.border_width The width of the border of the switch. 0
.style.button_color The color of the switch button.
.style.button_disabled_color The color of the switch button when it is disabled.
.style.button_hover_color The color of the switch button when it is hovered over.
.style.on_bg_color The background color of the switch when it is on.
.style.off_bg_color The background color of the switch when it is off.
.style.text_color The color of the text of the switch.
.style.text_disabled_color The color of the text of the switch when it is disabled.
.style.font_name The font of the text of the button. Either an installed font or a generic font name: "serif", "sans-serif", "monospace" or "system" "system"
.style.font_size The size of the text of the button. 12
.style.font_weight The weight of the text of the button. Either "normal" or "bold". "normal"
.style.font_style The style of the text of the button. Either "normal" or "italic". "normal"