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

  • Context: Python 
  • Thread starter Thread starter chaoseverlasting
  • Start date Start date
  • Tags Tags
    Code
Click For Summary
SUMMARY

The discussion focuses on the correct usage of the onkey() event handler in Python's Turtle graphics library for handling keyboard inputs. The user encountered issues with passing arguments to functions within the onkey() method. The solution involves defining wrapper functions that do not take parameters, as the onkey() method requires a function reference without arguments. This was confirmed by referencing the Python 3.4.2 documentation.

PREREQUISITES
  • Familiarity with Python programming language
  • Understanding of Turtle graphics library in Python
  • Knowledge of event-driven programming concepts
  • Basic understanding of function definitions and scope in Python
NEXT STEPS
  • Learn how to create wrapper functions in Python to handle arguments
  • Explore the Turtle graphics library documentation for advanced features
  • Investigate event handling in other Python libraries such as Pygame
  • Practice implementing keyboard input handling in various Python projects
USEFUL FOR

Python developers, educators teaching programming concepts, and anyone interested in interactive graphics programming using the Turtle library.

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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
Replies
55
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
1
Views
3K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
3K