Turtle gives error: AttributeError: 'Turtle' object has no attribute 'onkeyrelease'
Turtle gives error: AttributeError: 'Turtle' object has no attribute 'onkeyrelease'
我试图在每次释放密钥时加 1:
from turtle import *
import turtle
turtle1 = Turtle()
screen = turtle1.getscreen()
goPressed = False
进口龟...
currentWatts=0
def onaclicked():
global currentWatts
currentWatts+=1
print (currentWatts)
将我的函数定义为 运行 当键:1,被释放时
turtle.onkeyrelease(onaclicked, "1")
出于某种原因 onkeyrelease
不存在,即使我导入了 Turtle 并检查了 Python 文档。它应该工作,不是吗?我导入不当吗?你能帮帮我吗?
我希望它是 onkeyrelease
而不是 onkey
的原因是因为它用于游戏。使用 onkey
,当您将手指按住按键时,它会每 0.25 秒左右将 currentWatts 加 1。你可以通过在键上放置一些东西来作弊,所以我希望它只在你释放键时加 1。
您的代码有几个问题:您以两种不同的方式导入 turtle,这使事情变得混乱; onkeyrelease()
真的是一个方法screen/window,不是乌龟;您没有调用允许处理击键的 listen()
。以下应在 Python 3:
中工作
from turtle import Turtle, Screen, mainloop
def onaclicked():
global currentWatts
currentWatts += 1
print(currentWatts)
currentWatts = 0
screen = Screen()
screen.onkeyrelease(onaclicked, "1")
screen.listen()
mainloop()
确保在开始输入之前单击 window 一次以将其激活。
如果您使用的是 Python 2,我从您收到的错误消息中怀疑是这种情况,请将 Python 3 别名 onkeyrelease
替换为 onkey
:
The method Screen.onkeypress() has been added as a complement to
Screen.onkey() which in fact binds actions to the keyrelease event.
Accordingly the latter has got an alias: Screen.onkeyrelease().
此更改在两个版本中的效果应该相同。使用 onkeyrelease
而不是 onkey
并不能解决您对关键问题的指责。
when you hold your finger on the key, it adds 1 to
currentWatts every around 0.25 seconds. You could cheat by placing
something on the key so I want it only to add 1 when you release
自动按键重复似乎由操作系统处理,可能需要在 Python 外部禁用,具体取决于 OS。一些示例链接:
- Apple OSX: 设置按键的速度
重复
- Ubuntu: 关闭重复键
按
- X-Windows from Python: key repeat in
tkinter
我试图在每次释放密钥时加 1:
from turtle import *
import turtle
turtle1 = Turtle()
screen = turtle1.getscreen()
goPressed = False
进口龟...
currentWatts=0
def onaclicked():
global currentWatts
currentWatts+=1
print (currentWatts)
将我的函数定义为 运行 当键:1,被释放时
turtle.onkeyrelease(onaclicked, "1")
出于某种原因 onkeyrelease
不存在,即使我导入了 Turtle 并检查了 Python 文档。它应该工作,不是吗?我导入不当吗?你能帮帮我吗?
我希望它是 onkeyrelease
而不是 onkey
的原因是因为它用于游戏。使用 onkey
,当您将手指按住按键时,它会每 0.25 秒左右将 currentWatts 加 1。你可以通过在键上放置一些东西来作弊,所以我希望它只在你释放键时加 1。
您的代码有几个问题:您以两种不同的方式导入 turtle,这使事情变得混乱; onkeyrelease()
真的是一个方法screen/window,不是乌龟;您没有调用允许处理击键的 listen()
。以下应在 Python 3:
from turtle import Turtle, Screen, mainloop
def onaclicked():
global currentWatts
currentWatts += 1
print(currentWatts)
currentWatts = 0
screen = Screen()
screen.onkeyrelease(onaclicked, "1")
screen.listen()
mainloop()
确保在开始输入之前单击 window 一次以将其激活。
如果您使用的是 Python 2,我从您收到的错误消息中怀疑是这种情况,请将 Python 3 别名 onkeyrelease
替换为 onkey
:
The method Screen.onkeypress() has been added as a complement to Screen.onkey() which in fact binds actions to the keyrelease event. Accordingly the latter has got an alias: Screen.onkeyrelease().
此更改在两个版本中的效果应该相同。使用 onkeyrelease
而不是 onkey
并不能解决您对关键问题的指责。
when you hold your finger on the key, it adds 1 to currentWatts every around 0.25 seconds. You could cheat by placing something on the key so I want it only to add 1 when you release
自动按键重复似乎由操作系统处理,可能需要在 Python 外部禁用,具体取决于 OS。一些示例链接:
- Apple OSX: 设置按键的速度 重复
- Ubuntu: 关闭重复键 按
- X-Windows from Python: key repeat in tkinter