Turtle Gives Error: Attributeerror: 'turtle' Object Has No Attribute 'onkeyrelease'
Solution 1:
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 += 1print(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.
If you're using Python 2, which I suspect from the error message you got, then replace the Python 3 alias onkeyrelease
with 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().
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.
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
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:
- Apple OSX: Set how quickly a key repeats
- Ubuntu: Turn off repeated key presses
- X-Windows from Python: key repeat in tkinter
Post a Comment for "Turtle Gives Error: Attributeerror: 'turtle' Object Has No Attribute 'onkeyrelease'"