Skip to content

The Event Object

Whenever an event is triggered, GooeyPie calls the associated event function and passes it an event object as an argument. This object contains information about the event that occurred, such as which widget triggered the event, the type of event, and any relevant data (like mouse coordinates for a click event).

When events are triggered, an event function is called and sent an event object, which contains information about the event.

The event object has the following properties that you can access in your event function to get more information about the event:

PropertyTypeDescription
.widgetWidget objectThe widget that triggered the event. This allows you to access the properties of the widget that was interacted with.
.nameStringA string describing the type of event that occurred (e.g., "click", "change", "key_press").
.keyStringFor keyboard events, this property contains the key that was pressed.
.xIntegerFor mouse events, this property contains the x-coordinate of the mouse cursor at the time of the event relative to the widget.
.yIntegerFor mouse events, this property contains the y-coordinate of the mouse cursor at the time of the event relative to the widget.

In this example, we have two buttons that both update the value of a label. Instead of writing two separate event functions, we can use the event object to determine which button was clicked and update the label accordingly.

hello_goodbye.py
import gooeypie as gp
def update_greeting(event):
"""Updates the greeting message based on the user's name."""
# Determine whether the hello or goodbye button was pressed
if event.widget == say_hello_btn:
greeting = "Hello"
else:
greeting = "Goodbye"
# Get the name or use "friend" if the entry is empty
if name_entry.text:
name = name_entry.text
else:
name = "friend"
# Update the greeting label
greeting_lbl.text = f"{greeting}, {name}!"
app = gp.GooeyPieApp("Hello, You!")
# Create widgets
question_lbl = gp.Label("What's your name?")
name_entry = gp.Entry()
say_hello_btn = gp.Button("Say Hello", update_greeting)
say_goodbye_btn = gp.Button("Say Goodbye", update_greeting)
greeting_lbl = gp.Label()
# Make the label widget wider
name_entry.width = 200
# Add widgets to the window
app.add(question_lbl, 1, 1)
app.add(name_entry, 1, 2)
app.add(say_hello_btn, 1, 3)
app.add(say_goodbye_btn, 1, 4)
app.add(greeting_lbl, 1, 5)
app.run()