簡體   English   中英

我收到一條錯誤消息:_tkinter.TclError:錯誤的事件類型或鍵符“ButtonLeft”

[英]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 (右鍵)。 如果鼠標有更多按鈕,它可能還會使用45 它們被分配給海龜,它們需要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()

PEP 8 -- Python 代碼風格指南

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM