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

  • Thread starter chaoseverlasting
  • Start date
  • Tags
    Code
In summary: So in order to pass keyboard input to the change_size() function, you would have to hook up an onkey() event handler to the change_size() function, and pass in the key you're interested in as the first argument.
  • #1
chaoseverlasting
1,050
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
  • #2
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]
 
  • #3
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.
 

1. What is event handling code?

Event handling code is a set of instructions written in a programming language that controls how a computer program responds to various events or user actions, such as clicking a button or typing on a keyboard.

2. Why is event handling important in programming?

Event handling allows programs to interact with users in real-time, making them more user-friendly and responsive. It also helps in creating dynamic and interactive applications.

3. What is the basic structure of event handling code?

The basic structure of event handling code consists of three components: an event, an event listener, and an event handler. The event is the user action that triggers the event listener, which is responsible for detecting the event and calling the event handler, which contains the code that responds to the event.

4. How do you add event handling code to a web page?

Event handling code can be added to a web page using HTML event attributes, such as onclick, onmouseover, and onsubmit, which can be added to HTML elements in the <body> section of the page. Alternatively, event handling code can also be added using JavaScript in a <script> tag or an external JavaScript file.

5. What are some common event handling functions?

Some common event handling functions include onclick, which is triggered when a user clicks on an element, onmouseover, which is triggered when a user hovers over an element, and onsubmit, which is triggered when a user submits a form. Other common functions include onchange, onkeydown, and onkeyup.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
2
Views
916
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
931
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
890
  • Programming and Computer Science
Replies
4
Views
916
Back
Top