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 occurred.

The event data returned has:

Note: only data relevant to the event will be returned. e.g. key is only returned for when_key_# events and width and height are only returned for when_resized events.

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

widget.when_clicked = clicked


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(): = "lightblue"

def lowlight(): = "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