■ Canvas 클래스의 bind_all 메소드를 사용해 이벤트 핸들러를 설정하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from tkinter import * tk = Tk() canvas = Canvas(tk, width = 500, height = 500) canvas.pack() canvas.create_polygon(10, 10, 10, 60, 50, 35) def moveTriangle(event): if event.keysym == "Up": canvas.move(1, 0, -3) elif event.keysym == "Down": canvas.move(1, 0, 3) elif event.keysym == "Left": canvas.move(1, -3, 0) else: canvas.move(1, 3, 0) canvas.bind_all("<KeyPress-Up>" , moveTriangle) canvas.bind_all("<KeyPress-Down>" , moveTriangle) canvas.bind_all("<KeyPress-Left>" , moveTriangle) canvas.bind_all("<KeyPress-Right>", moveTriangle) tk.mainloop() |