User manual
85
The texts of the three patterns of choice are defined in a separate list form. Each of the three list elements
consists of a pair of values, each consisting of the text displayed and a numeric value, which is returned later
when the respective radio button is selected.
root = Tk(); root.title("LED")
The initialization of the root widget corresponds to the previous program, however, the contents of the
dialog box differ.
root = Tk(); root.title("LED"); v = IntVar(); v.set(1)
Variables that are used in Tk dialog boxes must be declared prior to first use and that is therefore contrary to
the normal Python variables. These two lines declare a variable
v as an integer and the initial value is set to
1.
def LedOn():
e = v.get()
Another function is defined, which is called LedOn() like in the last example. However, this time it does not
only turns on an LED, but also starts an LED pattern. The second function
LedOff() of the previous example
is not needed here. The first line of the new function reads the user input from the Tk variables
v and writes
the value to the Python variable
e. We will find out below in the explanation about radio buttons how
exactly the value gets into the variable
v.
Depending on the user’s selection, three different programme loops are started:
if e == 1:
for i in range(w):
for j in range(4):
GPIO.output(LED[j], True); time.sleep(t)
GPIO.output(LED[j], False)
In the first case, a loop will run through five times in succession, and will switch on each of the four LEDs for
0.2 seconds and off again. The five repetitions and the 0.2 seconds flashing time are defined by the variables
w und t at the beginning of the programme.
elif e == 2:
for i in range(w):
for j in range(4):
GPIO.output(LED[j], True)
time.sleep(t)
for j in range(4):
GPIO.output(LED[j], False)
time.sleep(t)
In the second case, all four LEDs are turned on simultaneously five times in succession and, after being lit for
0.2 seconds turn off again simultaneously.
else:
for i in range(w):
for j in range(4):