Di chuyển ảnh trên Canvas bằng phím mũi tên: bind các phím , , , gọi hàm canvas.move().
Ví dụ minh họa
from tkinter import *
from PIL import Image, ImageTk
win = Tk()
win.geometry("700x350")
canvas = Canvas(win, width=600, height=400, bg="white")
canvas.pack(pady=20)
image = ImageTk.PhotoImage(Image.open('favicon.ico'))
img = canvas.create_image(250, 120, anchor=NW, image=image)
def left(e):
canvas.move(img, -20, 0)
def right(e):
canvas.move(img, 20, 0)
def up(e):
canvas.move(img, 0, -20)
def down(e):
canvas.move(img, 0, 20)
win.bind("", left)
win.bind("", right)
win.bind("", up)
win.bind("", down)
win.mainloop()
Kết quả đầu ra
Ảnh di chuyển bằng phím mũi tên trên Canvas.
