AlexParry

Checking if the enter key was pressed in guizero

May 6th, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. from guizero import App, Text
  2.  
  3. # this function handles the when_key_pressed event
  4. def key_press_event(event):
  5.     # display a message which includes the key a user just pressed
  6.     text.value = "You pressed the {} key".format(event.key)
  7.  
  8.     # check if the <return> key was pressed
  9.     if (event.key == "\r"):
  10.         text.value = "The Enter key was pressed"
  11.  
  12. # create a new app
  13. app = App(title="Key Press Example")
  14.  
  15. # set the event handler function for the when_key_pressed event
  16. app.when_key_pressed = key_press_event
  17.  
  18. # create a text widget and give it a default value
  19. text = Text(app)
  20. text.value = "Press a key"
Add Comment
Please, Sign In to add comment