Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_basic_keypress.py
- from Tkinter import *
- from math import *
- import time
- root = Tk()
- canv = Canvas(root, height = 500, width = 500, bg = "grey")
- canv.pack()
- label = Label(canv, text="last key pressed: ...", width=20)
- label.pack(fill="both", padx=100, pady=100)
- # give keyboard focus to the label by default, and whenever
- # the user clicks on it
- label.focus_set()
- label.bind("<1>", lambda event: label.focus_set())
- def on_wasd(event):
- label.configure(text="last key pressed: " + event.keysym)
- for z in 'w a s d Up Down Left Right'.split():
- label.bind("<"+z+">", on_wasd)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement