Events

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 do_this():
    print("The widget was clicked")

widget.when_clicked = do_this

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()