Help, i'm trying to make a turtle graphics program that uses keyboard to control the direction of the turtle but every time i open it, it keeps crashing!!
import keyboard
from turtle import *
keyboard.add_hotkey('Up arrow', lambda:
forward(2))
keyboard.add_hotkey('Left arrow', lambda:
left(2))
keyboard.add_hotkey('right arrow', lambda:
right(2))
First, we don't need the keboard module to move the turtle around the screen with the keyboard, we can use turtle's own keyboard events:
Make sure to click on the window to activate it before typing. If you really want to use the keybard module, this seems to have worked for me:
You can also use a
lambda
variant:Though on my system (OSX) it was difficult to work with (single key press generated multiple events that didn't fire until later key presses, wanted to run as admin, etc.)