User manual

77
pygame.display.update() So far, all the graphic elements have been drawn only virtually. But this line
restructures the graphic display genuinely. It is updated simultaneously. Therefore, you can’t see any
flickering when the individual pointers are drawn in sequence.
for event in pygame.event.get():
if event.type == QUIT or (event.type ==
KEYUP and event.key == K_ESCAPE):
mainloop = False
We are still within the if query, where once per second, the relatively performance-hungry query attempts
to detect any system events to verify whether the user wanted to close the window of the clock within the
last second or has pressed the
[Esc] key. If that is the case, the variable mainloop is set to False and the
loop will not be restarted.
pygame.quit() This last line terminates the PyGame module first, so that the graphical window closes and
then the entire application.
10 Graphical dialog fields for program control
There is no modern program that requires some interaction with the user and is running in pure text mode.
Graphical user interfaces, where you click on buttons, instead of having to enter data via the keyboard are
abundant.
Python itself has no graphical user interface for programmes, but we have got several external modules,
similar to the PyGame described earlier which are particularly meant for creating graphical interfaces. One
such modules is the famous
Tkinter
, which produces the graphical interface
Tk
for Python, and is also of use
for various other programming languages.
The structures of the graphical toolkit Tk are slightly different from Python and may look a bit unfamiliar at
first. So let’s start with a very simple example: An LED is switched on and off by buttons in a dialog box.
Components required:
1x breadboard
1x LED red
1x 220-ohm series resistor
2x connecting wire