[英]Tkinter is giving me a _tkinter.TclError: bad event type or keysym "button" when i try to run it
[英]I'm getting an error message: _tkinter.TclError: bad event type or keysym "ButtonLeft"
我的代碼:
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()
我該如何解決? 我也更喜歡使用 onkey 而不是 onrelease 但它給了我一條錯誤消息: 'Turtle' object has no attribute 'onkey'
onrelease()
、 onclick()
、 ondrag()
用於鼠標按鈕 - 它需要數字1
(左鍵)、 2
(中鍵)、 3
(右鍵)。 如果鼠標有更多按鈕,它可能還會使用4
和5
。 它們被分配給海龜,它們需要a.onrelease()
onkey( onkey()
、 onkeyrelease()
、 onkeypress()
用於鍵盤的鍵 - 但鍵未分配給海龜,您應該直接使用turtle.onkey()
或者您應該創建screen = turtle.Screen()
並使用screen.onkey()
而且您不必在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.