Advertisement
here2share

# Tk_basic_keypress.py

Jul 31st, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. # Tk_basic_keypress.py
  2.  
  3. from Tkinter import *
  4. from math import *
  5. import time
  6.  
  7. root = Tk()
  8. canv = Canvas(root, height = 500, width = 500, bg = "grey")
  9. canv.pack()
  10.  
  11. label = Label(canv, text="last key pressed:  ...", width=20)
  12. label.pack(fill="both", padx=100, pady=100)
  13.  
  14. # give keyboard focus to the label by default, and whenever
  15. # the user clicks on it
  16. label.focus_set()
  17. label.bind("<1>", lambda event: label.focus_set())
  18.  
  19. def on_wasd(event):
  20.     label.configure(text="last key pressed: " + event.keysym)
  21.  
  22. for z in 'w a s d Up Down Left Right'.split():
  23.     label.bind("<"+z+">", on_wasd)
  24.  
  25. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement