Raspberry_Pi_Education_Manual

Notes:

Up to this point, the data being processed has been printed to the screen; but it could have easily been printed on paper or written out to a file. We’ve been putting text directly onto the console output. If you want to draw onto a window you will need to use a graphical window library. In this section we are using the “PySide” library (a version of the “Qt4” library), although another – called “Tk” – comes included with every version of Python. When you tell a computer to change the image it’s displaying, it does this by changing the colour of pixels on the computer screen. As with almost all computers today, the display on a Raspberry Pi is separated into what are called “windows”. These windows “look” onto a drawable surface. Everything you see on a graphical computer display is actually a drawing within a window; even the drawing of text in your web browser. A window must be controlled by an application (an app), so this is created first. The application must be running for the window to be displayed and, for this, it must hang around in a never-ending loop: the main loop. Before looping, you define how you want the window to look and how each part will react to stimulus (input). Finally you simply tell it to display. To respond to external influences, such as mouse movement or keyboard key presses, you must override event functions. Some “ widgets ” (such as push buttons) can connect a function to a signal, such as “clicked”.

In the experiment, the two methods for detecting changes are demonstrated. When the button is clicked, the “buttonClicked” function is called. The other is responding to the mouse cursor moving over the widget and changing the label to show the mouse position (but only when on the window, not when on the button).

Experiments in Python

95

Made with FlippingBook flipbook maker