User manual

79
Button(root, text=”Off", command=LedOff).pack(side=LEFT)
root.mainloop()
GPIO.cleanup()
Fig. 10.2: The final dialog box will look like this.
10.1.1 How does it work?
This programme shows the basic functions of the Tkinter library for the building graphical dialogs. The size
of the dialog boxes and controls in Tkinter is acquired automatically according to the size that is desired and
can also be modified manually later on, if necessary unlike the graphics library PyGame where graphics are
constructed accurately pixel by pixel.
import RPi.GPIO as GPIO
from Tkinter import *
After having imported the GPIO library, we also import the elements of the Tkinter library.
LED = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED,GPIO.OUT)
The lines are familiar. The GPIO port 4 is defined as output port for an LED and is denoted by the LED
variables
LED.
def LedOn():
GPIO.output(LED,True)
The function LedOn() which turns the LED on, is now defined.
def LedOff():
GPIO.output(LED,False)
A similar function, LedAus(), turns the LED off. These two functions are called by the two buttons in the
dialog box at a later stage.
Up to here we had plain Python, now we continue with Tk and its peculiarities.
root = Tk() Tkinter works with so-called widgets. These are independent screen elemente, in most cases
these are to dialog boxes, which in turn contain various elements. Each programme needs a
root widget,
that calls up all the other objects. This
root widget is always named Tk(), it automatically generates a
window and also initializes the Tkinter library.