Python onkey/onkeypress function activating without me pressing any keys
22 minutes ago, Linus No Beard said:It looks like your code is creating a new turtle and assigning it a letter every time the function
havefunis called. It appears that thehavefunfunction is called 5 times when the code first runs, which is why you see the letters being drawn without any input from you.If you want the letters to only be drawn when a key is pressed, you can move the call to
havefuninside of a function that is called when a key is pressed. You can use theonkeypressmethod of theScreenobject to register a function to be called when a key is pressed.
That's not specifically what I was talking about, but I want those 5 turtles objects created, as they are the first 5 turtles you use to start the "game" so to speak. The problem I was having is that the "onkeypress" function was running the callback without the key being pressed.
Anyways, I figured out a solution, it looks like this:
wn.onkey(lambda n = "A": keyfilter(n),"A")
The onkey function does not support passthrough. So what was happening is that the function was being called without the key needing to be pressed. I am not clear on how this "lambda" thing works, but it does, so...

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now