The SimpleGUI module in CodeSkulptor 3 was created specifically to help students learn and experiment with event-based interactions. It was designed to work in a web version of Python called CodeSkulptor 3. Unfortunately, SimpleGUI is not a module that works inside the Runestone textbook, it only works in CodeSkulptor. Here is a screenshot of what CodeSkulptor 3 looks like when you first open it, and press the Run button at the top-left: https://py3.codeskulptor.org/
This screenshot shows that CodeSkulptor 3 provides a window for entering text and a sample script to get you started. On the left are buttons for you to control the CodeSkulptor web page interface, including running, saving and loading scripts, accessing documentation, and a βjoinβ button that allows you to share access to your script with a classmate, so that you can work on it together.
When you run the starter script that is provided, you will also see that a window pops up. This window has a canvas and a button. When this window first appears, it displays the message βWelcome!β. When you click the βClick meβ button, the message in the canvas changes to βGood job!β.
An event handler function defines what happens when the user clicks the button with line 13 giving access to the global variable message, and then line 14 changes the value of that variable to "Good job!"
A draw() event handler function includes a function canvas.draw_text() to specify what should be drawn on the canvas. The SimpleGUI module calls this draw() function automatically, many times per second, to redraw the canvas and keep it up to date as the user interacts with the program.
A window is created using the create_frame method of the SimpleGUI module. It gives the window a title (βHomeβ) and a size in pixels for a canvas that will be drawn on the right side of the window (the canvas in this default example is 300 pixels wide by 200 pixels tall). The window that is created is assigned to a variable called βframeβ. When you create a frame in SimpleGUI, you get an area on the left for placing interface controls and an area on the right for the canvas.
Then a button is added to βframeβ, with the label βClick meβ, and it also registers that when the button is clicked by the user, the function βclick()β should be called.
Go to CodeSkulptor 3 and play with this code: https://py3.codeskulptor.org/. Run the script and then do a few simple edits, testing the program after each:
Rename the button handler to button_click instead of click. Make sure you update this in the function definition and in the line of code that creates the button.
Notice that the window that pops up when you run a CodeSkulptor script with the SimpleGUI module has a key and mouse indicator box at the bottom left of the control panel. These are provided by SimpleGUI as an aid to help novice programmers understand what mouse and keyboard events the SimpleGUI module is receiving.
Later in this chapter, we will define and register event handler functions for mouse and keyboard events. You will then be able to observe how values displayed within these indicators change based on mouse clicks/keypresses.