Python tkinter 用键盘移动没反映修改代码

from tkinter import *

 
def movetriangle(event):
    if event.keysym == 'Up':
        canvas.move(a1,0,-3)
    elif event.keysym == 'Down':
        canvas.move(a1,0,3)
    elif event.keysym == 'Left':
        canvas.move(a1,-3,0)
    elif event.keysym == 'Right':
        canvas.move(a1,3,0)
 
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
a1=canvas.create_polygon(10,10,10,60,50,35)
canvas.bind_all('<KeyPress-Up>',movetriangle)
canvas.bind_all('<KeyPress-Down>',movetriangle)
canvas.bind_all('<KeyPress-Left>',movetriangle)
canvas.bind_all('<KeyPress-Right>',movetriangle)
canvas.pack()
posted @ 2018-01-17 09:58  追忆丶年华  阅读(297)  评论(0编辑  收藏  举报