As demonstrated in the default code on the main page of CodeSkulptor, the button event handler function, def click(), does not take any parameters. It is simply called when the button is clicked. The documentation shows that the text input event handler function, def input_handler(text_input), requires one parameter, which is how it receives the string of text the user enters.
Unlike buttons and text input boxes, which can only respond to one type of event, the window frame can recognize any of the following four types of events:
The mouse events are triggered if the clicks or drags are on the canvas and the key events are recognized when the location of the mouse pointer is within the canvas area. This section shows examples of how to register and write these event handler functions.
This documentation shows us that in order to register a mouse click handler function, we need to access the set_mouseclick_handler() function on a frame object with the name, mouse_handler, being passed as an argument:
Notice that this event handler function, def mouse_handler(position), requires one parameter. You might be thinking that is weird because a mouse click happens in a two-dimensional window and so you need to specify an x and a y coordinate. But in SimpleGUI these coordinates are passed as a two-item tuple (remember tuples? the lists that canβt be modified?). So, if the mouse was clicked at 25 pixels to the right of the top-left corner and 100 pixels down from the top-left corner, the tuple (25,100) will be passed to the mouse click event handler.
The code above is similar to the examples we have seen already - a message is displayed on screen and can be changed if a user types a new message in the input box. But, it also draws a blue circle (outline only) on the canvas wherever the user last clicked. Note that there is one parameter in the mouse_click() function, called βposβ and we print this out to the output console, but also assign it to the global variable βcirc_posβ. That global variable is used in the draw() function to draw a circle.
Letβs now look at how SimpleGUI handles keyboard events, because it is also different than the way the Turtle module handled keyboard events. Review the key event handler documentation which is located below:
For both of these you must specify the name of the function that will handle the event, and this event handler function, def key_handler(key), requires a single parameter to be passed which is the key that is pressed down (or released).
Click on the following link to review the key echo code example: https://py3.codeskulptor.org/#examples3_simplegui_key_echo.py. It is also pictured below with one extra print statement added on line 16. Add this print statement on line 16 in the keydown() handler function and run the code.
ASCII (American Standard Code for Information Interchange) is a standard way of using integer values to represent characters across many computer systems. For example, the letter βAβ has the ASCII value of 65.
When you press down on a key, that key parameter is passed as an ASCII numeric value to the keydown() handler function. However, if you also want the letter to be displayed on the canvas, this ASCII numeric value needs to be converted to a character type using chr() and then stored in the global variable current_key. That variable, current_key, is then used in the draw() function to display the letter on the canvas.
When you lift your finger off that same key, the keyup() handler is called and the global variable is then reset to the space key, resulting in the letter disappearing from the canvas. The print statement (added on line 16) allows the numeric value of the key parameter (ASCII value) to be displayed in the console. Notice how these values change as you type different letters.