■ Canvas 클래스 : bind_all 함수를 사용해 이벤트 핸들러 설정하기

----------------------------------------------------------------------------------------------------

from tkinter import *

 

tk = Tk()

 

canvas = Canvas(tk, width = 400, height = 400)

 

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)

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요