Python How can I properly use the onkey() event handler for keyboard inputs in Python?

  • Thread starter Thread starter chaoseverlasting
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion revolves around issues with registering keyboard inputs in a Python Turtle graphics program. The user is attempting to change the turtle's color and pen size using keyboard events but encounters problems due to incorrect function calls in the `onkey()` method. The user initially uses `wn.onkey(change_size(t,1), "+")`, which executes the function immediately rather than passing it as a reference. The correct approach involves defining the `onkey()` function with no arguments, allowing it to be called when the specified key is pressed. The user seeks clarification on how to properly pass arguments to the functions when using keyboard inputs, referencing Python 3.4.2 documentation that outlines the requirements for the `onkey()` event handler.
chaoseverlasting
Messages
1,050
Reaction score
3
Hi,

I'm trying to take keyboard inputs to make changes to a turtle from the 'Turtle' graphics in Python.

I'm not able to register keyboard inputs for the code I've written. I've tested out the examples that had been provided online, and they seem to work fine. I'm not quite sure where I'm going wrong.

Please have a look at the code and let me know your thoughts:

Code:
import turtle

t = turtle.Turtle()

turtle.setup(1200,720)

t.pensize(3)
t.color("red")
t.speed(3)
t.shape()

colour = ["red","green","blue"]        #turtle can be of three colours

wn = turtle.Screen()
wn.title("Turtle Lives!")def change_colour(t,k):                #change colour of turtle according to array
    t.color(colour[k])

pen_size = 3

def change_size(t,k):                #increase / decrease turtle pen_size
    global pen_size                    #modifying global pen_size variable
    if pen_size > 0:
        if k==1:
            pen_size +=1
        elif k==0:
            pen_size -=1
    t.pensize(pen_size)wn.onkey(change_colour(t,0),"r")    #Change colour
wn.onkey(change_colour(t,1),"g")
wn.onkey(change_colour(t,2),"b")

wn.onkey(change_size(t,1),"+")        #Increase / Decrease size
wn.onkey(change_size(t,0),"-")

wn.listen()
wn.mainloop()
 
Technology news on Phys.org
Hi,

I've identified the bug: the onkey() function isn't passing arguments through to the code.

I.e.
Code:
wn.onkey(change_size(t,1) ,"+")

#has to be altered to

change_size(change_size,"+")

How can I pass these arguments through the keyboard inputs?[/code]
 
chaoseverlasting said:
Hi,

I've identified the bug: the turtle.onkey() function isn't passing arguments through to the code.

I.e.
Code:
wn.onkey(change_size(t,1) ,"+")

#has to be altered to

change_size(change_size,"+")

How can I pass these arguments through the keyboard inputs?[/code]
According to the Python 3.4.2 documentation that I have, the onkey() event handler (I think this is the right terminology) takes two arguments. The first is the name of a function, with no arguments, and the second is the key you're interested in.
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top