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()