[英]I'm getting an error message: _tkinter.TclError: bad event type or keysym "ButtonLeft"
My code:我的代码:
def left():
a.bk(25)
def right():
a.fd(25)
def up():
a.lt(90)
a.fd(25)
a.rt(90)
def down():
a.rt(90)
a.fd(25)
a.lt(90)
while True:
a.onrelease(left,"Left")
a.onrelease(right,"Right")
a.onrelease(up,"Up")
a.onrelease(down,"Down")
a.listen()
How do I fix this?我该如何解决? I'd also prefer to use onkey instead of onrelease but it gives me an error message: 'Turtle' object has no attribute 'onkey'
我也更喜欢使用 onkey 而不是 onrelease 但它给了我一条错误消息: 'Turtle' object has no attribute 'onkey'
onrelease()
, onclick()
, ondrag()
are for mouse's buttons - and it needs numbers 1
(left button), 2
(middle button), 3
(right button). onrelease()
、 onclick()
、 ondrag()
用于鼠标按钮 - 它需要数字1
(左键)、 2
(中键)、 3
(右键)。 Probably it may also use 4
and 5
if mouse has more buttons.如果鼠标有更多按钮,它可能还会使用
4
和5
。 They are assigned to turtles and they need a.onrelease()
它们被分配给海龟,它们需要
a.onrelease()
onkey()
, onkeyrelease()
, onkeypress()
are for keyboard's keys - but keys are not assigned to turtles and you should use directly turtle.onkey()
or you should create screen = turtle.Screen()
and use screen.onkey()
onkey(
onkey()
、 onkeyrelease()
、 onkeypress()
用于键盘的键 - 但键未分配给海龟,您应该直接使用turtle.onkey()
或者您应该创建screen = turtle.Screen()
并使用screen.onkey()
And you don't have to run code in while
-loop but use turtle.mainloop()
or turtle.done()
.而且您不必在
while
循环中运行代码,而是使用turtle.mainloop()
或turtle.done()
。
import turtle
# --- functions --- # PEP8: all functions before main code
def left():
a.bk(25)
def right():
a.fd(25)
def up():
a.lt(90)
a.fd(25)
a.rt(90)
def down():
a.rt(90)
a.fd(25)
a.lt(90)
def turle_clicked(x, y):
print('turtle:', x, y)
def screen_clicked(x, y):
print('screen:', x, y)
# --- main ---
a = turtle.Turtle()
a.onrelease(turle_clicked, 1) # left button (clicked on turtle)
a.onrelease(turle_clicked, 2) # middle button (clicked on turtle)
a.onrelease(turle_clicked, 3) # right button (clicked on turtle)
turtle.onscreenclick(screen_clicked, 1) # left button (clicked in any place in window)
turtle.onkey(left,"Left")
turtle.onkey(right,"Right")
turtle.onkey(up,"Up")
turtle.onkey(down,"Down")
turtle.listen()
turtle.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.