Events

Warning - events are currently experimental, issues maybe experienced and the API may change..

Custom events can be added to guizero widgets to call functions when the user takes the following actions:

Events are set by assigning them to a function:

def clicked():
    print("clicked")

widget.when_clicked = clicked

Event Data

The function which is called can also accept a parameter and will be passed data about the event which occured.

The event data returned has:

def clicked(event_data):
    print("widget clicked = " + event_data.widget)
    print("mouse position = " + event_data.x + "." + event_data.y)

widget.when_clicked = clicked

Example

Highlight a text box widget by changing its background color (bg) when the mouse is hovering over it.

from guizero import App, TextBox

def highlight():
    text_box.bg = "lightblue"

def lowlight():
    text_box.bg = "white"

app = App()
text_box = TextBox(app)

# when the mouse enters the textbox
text_box.when_mouse_enters = highlight
# when the mouse leaves the textbox
text_box.when_mouse_leaves = lowlight

app.display()