Skip to content

Button

Buttons are for clicking! They’re the definitive way to invite users to take an action in your app.

Every button needs a job to do, so you’ll tell it which function to run when it’s clicked (but you can always set this to None if you are just designing the layout of your interface, in which case pressing the button will have no effect).

Examples

Hello GooeyPie!

The ‘Hello, World!’ of GooeyPie, this example shows how to create a button and have it update a label when clicked.

hello_gooeypie.py
import gooeypie as gp
def say_hello(event):
"""Changes the label text to "Hello GooeyPie!"."""
hello_lbl.text = "Hello GooeyPie!"
app = gp.GooeyPieApp("Hi!")
app.width = 300
# Create a button and an empty label
hello_btn = gp.Button("Say hello", say_hello)
hello_lbl = gp.Label("")
# Add the button and label to the app
app.add(hello_btn, 1, 1)
app.add(hello_lbl, 1, 2)
app.run()

The Clicker

In this app, the label of the button updates to show how many times it’s been clicked, but after 10 clicks the button gets tired and is set to disabled so it can’t be clicked anymore.

clicker.py
import gooeypie as gp
app = gp.GooeyPieApp("Clicker")
clicks = 0 # Number of times the button has been clicked
def click(event):
"""Increments the number of clicks and updates the button text."""
global clicks
clicks += 1
if clicks < 10:
clicker_btn.text = f"You have clicked me {clicks} times!"
else:
clicker_btn.text = "Okay, that's enough for one day"
clicker_btn.disabled = True
# Create the button
clicker_btn = gp.Button("You have not clicked me yet!", click)
clicker_btn.width = 250
# Add button to the app
app.add(clicker_btn, 1, 1)
app.run()

Creating a Button widget

my_button = gp.Button(button_text, event_function)

Parameters

  • button_text (string): The text of the button.
  • event_function (function): The event function that will be called when the button is pressed. This function must accept a single argument for the event object.

Properties

Name Type Description
.text string Gets or sets the text of the button.
.width integer Gets or sets the width of the button in pixels.
.height integer Gets or sets the height of the button in pixels.
.disabled boolean Gets or sets whether the button is disabled. When disabled is set to True, the button does not respond to click events.

Methods

Name Returns Description
.activate() None Activates the button as if it had been clicked.

Events

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

Name Description
.on_activate(event_function) The function to call when the button is pressed. This event should be set when the button is first created.

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 button. Will not be visible unless border_width is greater than 0. "transparent"
.style.border_width The width of the border of the button. 0
.style.button_color The color of the button.
.style.button_disabled_color The color of the button when it is disabled.
.style.button_hover_color The color of the button when it is hovered over.
.style.corner_radius The radius of the corners of the button. 6
.style.padding Additional space around the text of the button 0
.style.text_color The color of the text of the button.
.style.text_disabled_color The color of the text of the button 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 thenpm run dev button. Either "normal" or "italic". "normal"