How to have a function called when Enter key is pressed in a guizero application?
You can call a function on key press using .when_key_pressed
as mentioned in the docs.
def enterKeyClicked(event): if event.key == "\r": dbInsert()input = TextBox(app, width = 30, align = "top")input.when_key_pressed = enterKeyClicked
When you press any key, enterKeyClicked
is called and a guizero EventData
object is passed to it. You can use the .key
attribute of the event to get the character of the key pressed. If the key pressed is Enter then the character is "\r"
. This is the python character for a carriage return, which is returned when you press Enter. Once you've checked the Enter key has been pressed, you can then call dbInsert()
.