Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_bind_by_key.py
- try:
- import tkinter as tk # using Python 3
- except ImportError:
- import Tkinter as tk # using Python 2
- class Cv(): pass
- cv=Cv()
- def mvU(event):
- cv.y1 -= 10
- cv.y2 -= 10
- mv()
- def mvD(event):
- cv.y1 += 10
- cv.y2 += 10
- mv()
- def mvL(event):
- cv.x1 -= 10
- cv.x2 -= 10
- mv()
- def mvR(event):
- cv.x1 += 10
- cv.x2 += 10
- mv()
- def mv():
- oval = (cv.x1, cv.y1, cv.x2, cv.y2)
- canvas.coords(oval_id, oval)
- m = tk.Tk()
- canvas = tk.Canvas(m)
- canvas.pack(expand=1, fill='both')
- cv.x1, cv.x2 = 200, 250
- cv.y1, cv.y2 = 100, 150
- oval = (cv.x1, cv.y1, cv.x2, cv.y2)
- oval_id = canvas.create_oval(oval)
- canvas.bind_all('<w>', mvU)
- canvas.bind_all('<s>', mvD)
- canvas.bind_all('<a>', mvL)
- canvas.bind_all('<d>', mvR)
- m.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement