Advertisement
here2share

# Tk_bind_by_key.py

Aug 9th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. # Tk_bind_by_key.py
  2.  
  3. try:
  4.     import tkinter as tk  # using Python 3
  5. except ImportError:
  6.     import Tkinter as tk  # using Python 2
  7.  
  8. class Cv(): pass
  9. cv=Cv()
  10.  
  11. def mvU(event):
  12.     cv.y1 -= 10
  13.     cv.y2 -= 10
  14.     mv()
  15.  
  16. def mvD(event):
  17.     cv.y1 += 10
  18.     cv.y2 += 10
  19.     mv()
  20.  
  21. def mvL(event):
  22.     cv.x1 -= 10
  23.     cv.x2 -= 10
  24.     mv()
  25.  
  26. def mvR(event):
  27.     cv.x1 += 10
  28.     cv.x2 += 10
  29.     mv()
  30.  
  31. def mv():
  32.     oval = (cv.x1, cv.y1, cv.x2, cv.y2)
  33.     canvas.coords(oval_id, oval)
  34.  
  35. m = tk.Tk()
  36.  
  37. canvas = tk.Canvas(m)
  38. canvas.pack(expand=1, fill='both')
  39. cv.x1, cv.x2 = 200, 250
  40. cv.y1, cv.y2 = 100, 150
  41. oval = (cv.x1, cv.y1, cv.x2, cv.y2)
  42. oval_id = canvas.create_oval(oval)
  43. canvas.bind_all('<w>', mvU)  
  44. canvas.bind_all('<s>', mvD)  
  45. canvas.bind_all('<a>', mvL)  
  46. canvas.bind_all('<d>', mvR)  
  47.  
  48. m.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement