Raspberry_Pi_Education_Manual

Notes:

To draw a line, or a group of lines, first requires binding the paint event to a function. Hence, when the windowing system is ready to draw to the window it will call your function. Before going to the main loop, we need to set up a QWidget within the app. This experiment and the last introduced the concept of a “ class ”. Understanding a class is quite complicated but think of it as a block of data that also defines the functions that can manipulate that data. You can create multiple blocks of data with the same class and they will all have the same functions being carried on different data. A program is basically a class with functions and classes within it. The “SketchWindow” is a type of QWidget, and it will share all the functions had by a QWidget. The SketchWindow adds a new way to initialise (__init__) or setup the QWidget. It also defines a new way to draw the content of the widget. That is all we have to change, the normal function of the QWidget remains the same and so we don’t have to write that code too. Notice how you’ve overridden the “paintEvent” with your own painting function. This paint function sets a yellow pen and then uses the drawing function that was given to the sketch window. The drawing function draws a sine curve five times, between the left edge and the right edge of the window frame. It does this by creating a list of points representing the curve. Sine can go from -1 to +1 but the calculation makes this range from 0 to 200. Finally, it joins up all the points using a “Polyline” to make a sine curve. In this and the last experiment, you will have noticed that the top left of the screen and the content of our window is at coordinates 0, 0. When drawing on graph paper the origin (0,0) is at the bottom left. It is important to remember this when understanding how to define the coordinates of elements of your window.

Computers address the screen with the coordinate origin (0,0) at the top left, unlike drawing a graph.

Experiments in Python

97

Made with FlippingBook flipbook maker