海龟给出错误:AttributeError:'Turtle' 对象没有属性 'onkeyrelease' [英] Turtle gives error: AttributeError: 'Turtle' object has no attribute 'onkeyrelease'

查看:59
本文介绍了海龟给出错误:AttributeError:'Turtle' 对象没有属性 'onkeyrelease'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次释放一个键时都试图加 1:

I am trying to add 1 every time I release a key:

from turtle import *
import turtle
turtle1 = Turtle()
screen = turtle1.getscreen()
goPressed = False

进口海龟...

currentWatts=0

def onaclicked():
     global currentWatts
     currentWatts+=1
     print (currentWatts)

定义我的函数在 key: 1, 被释放时运行

defined my function to be run when the key: 1, is released

turtle.onkeyrelease(onaclicked, "1")

由于某种原因 onkeyrelease 不存在,即使我导入了 Turtle 并检查了 Python 文档.它应该有效,不是吗?我是不是导入不当?你能帮我吗?

for some reason onkeyrelease isn't there even though I imported Turtle and checked in Python documentation. It SHOULD work, shouldn't it? Did I improperly import? Can you please help me?

我希望它是 onkeyrelease 而不是 onkey 的原因是因为它是用于游戏的.使用 onkey,当您将手指放在按键上时,它每大约 0.25 秒将 currentWatts 加 1.你可以通过在钥匙上放一些东西来作弊,所以我希望它在你松开钥匙时只加 1.

The reason I want it to be onkeyrelease instead of onkey, is because it is for a game. With 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 the key.

推荐答案

您的代码有几个问题:您以两种不同的方式导入了turtle,这使事情变得混乱;onkeyrelease() 真的是屏幕/窗口的方法,不是乌龟;您没有调用允许处理击键的 listen().以下应该适用于 Python 3:

You've several problems with your code: you import turtle two different ways which confuses things; onkeyrelease() is really a method of the screen/window, not a turtle; you didn't call listen() which allows keystrokes to be processed. The following should work in 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()

确保在开始输入之前单击该窗口一次以使其处于活动状态.

Make sure to click on the window once before you start typing to make it active.

如果您使用的是 Python 2,我从您收到的错误消息中怀疑,那么将 Python 3 别名 onkeyrelease 替换为 onkey:

If you're using Python 2, which I suspect from the error message you got, then replace the Python 3 alias onkeyrelease with onkey:

方法 Screen.onkeypress() 已添加为对Screen.onkey() 实际上将动作绑定到 keyrelease 事件.因此后者有一个别名:Screen.onkeyrelease().

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 并不能解决关键问题.

This change should work the same in both versions. Using onkeyrelease instead of onkey wasn't going fix your holding a finger on the key issue.

当您将手指放在琴键上时,它会加 1currentWatts 每大约 0.25 秒.你可以通过放置作弊键上的东西,所以我希望它只在你释放时加 1

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 外部禁用,具体取决于操作系统.一些示例链接:

It appears that automatic key repeats are handled by the operating system and may need to be disabled external to Python, depending on the OS. Some example links:

这篇关于海龟给出错误:AttributeError:'Turtle' 对象没有属性 'onkeyrelease'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆