Python tkinter 用键盘移动没反映,如何修改代码
以下代码怎么没效果啊,谁能帮一下,目的是按方向键移动一个三角箭头。fromtkinterimport*tk=Tk()canvas=Canvas(tk,width=400,...
以下代码怎么没效果啊,谁能帮一下,目的是按方向键移动一个三角箭头。
from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
a1=canvas.create_polygon(10,10,10,60,50,35)
def movetriangle(event):
if event.keysym == 'up':
canvas.move(a1,0,-3)
elif event.keysym == 'Down':
canvas.move(a1,0,3)
elif keysym == 'Left':
canvas.move(a1,-3,0)
elif keysym == 'Right':
canvas.move(a1,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) 展开
from tkinter import *
tk=Tk()
canvas=Canvas(tk,width=400,height=400)
canvas.pack()
a1=canvas.create_polygon(10,10,10,60,50,35)
def movetriangle(event):
if event.keysym == 'up':
canvas.move(a1,0,-3)
elif event.keysym == 'Down':
canvas.move(a1,0,3)
elif keysym == 'Left':
canvas.move(a1,-3,0)
elif keysym == 'Right':
canvas.move(a1,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) 展开
展开全部
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()
#你的代码要么变量取错,要么单词写错,好好比较一下吧,看看和你的有什么不一样
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询