Saturday, February 8, 2014

Una piccola applicazione multiframe in Tkinter

All'interno di una finestra è possibile ospitare più Frame, ognuna con i suoi widget. La piccola applicazione di cui allego il codice crea tre Frame, ognuna delle quali ospita un Button e una Label.

L'applicazione disegna un semaforo. Cliccando su uno qualsiasi dei Button si ciclano i colori come in un vero semaforo, nella label compare lo stato della lampada a fianco.

L'evento <click> del mouse su un button chiama la routine 'change', che ha il compito di aggiornare lo stato delle luci e delle label collegate.

Nonostante sia una piccola cosa, mostra alcune tecniche interessanti: la gestione dell'evento click del mouse, il cambiamento di stato di variabili d'istanza relative ai widget; inoltre l''uso dei caratteri unicode, oltre che per l'inserimento degli specifici caratteri di una lingua più o meno esotica, ritorna utile per l'uso di simboli più particolari, ad esempio quelli astrologici, che ritroveremo più avanti. Nel caso dell'applicazione, ho usato un carattere unicode per disegnare un pallino pieno che potesse assomigliare alla luce di un semaforo, colorandolo opportunamente.

Notate inoltre l'uso della geometry pack per i frame, che allinea verticalmente i widget, se non altrimenti specificato. Le label sono invece collocate a sinistra del button: .pack(side='left')

La configurazione di ogni widget viene modificata attraverso l'opzione config(ure), che serve anche a leggere le proprietà già settate, come si vede nel testo dello script. Per aiutare a comprendere il senso dell'espressione

b1_c=self.button1.config('foreground')[-1]
, sottolineo semplicemente che l'espressione self.button1.config restituisce una tupla (cioè un array non modificabile di valori) di cui l'ultimo rappresenta il valore che ci interessa, cioè il colore di foreground del pallino, da cui il [-1].


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf-8 -*-
import Tkinter

class MyApp:

    def __init__(self, myProperty):
        self.myProperty = myProperty
        # Frame n° 1
        self.frame1=Tkinter.Frame()
        self.frame1.pack()
        self.button1=Tkinter.Button(self.frame1,
                                    font = "Courier 36",
                                    text = u"\u25cf",
                                    foreground="grey",
                                    activeforeground="grey",
                                    command=self.change)
        self.button1.pack(side='left')
        self.label1=Tkinter.Label(self.frame1,
                                  text="spento"
                                  )
        self.label1.pack(side='left')

        # Frame n° 2
        self.frame2=Tkinter.Frame()
        self.frame2.pack()
        self.button2=Tkinter.Button(self.frame2,
                                    font = "Courier 36",
                                    text = u"\u25cf",
                                    foreground="grey",
                                    activeforeground="grey",
                                    command=self.change)
        self.button2.pack(side='left')
        self.label2=Tkinter.Label(self.frame2,
                                  text="spento"
                                  )
        self.label2.pack(side='left')

        # Frame n° 3
        self.frame3=Tkinter.Frame()
        self.frame3.pack()
        self.button3=Tkinter.Button(self.frame3,
                                    font = "Courier 36",
                                    text = u"\u25cf",
                                    foreground="green",
                                    activeforeground="green",
                                    command=self.change)
        self.button3.pack(side='left')
        self.label3=Tkinter.Label(self.frame3,
                                  text="acceso"
                                  )
        self.label3.pack(side='left')
 
    def change(self, *argv):

        self.label1.config(text='spento')
        self.label2.config(text='spento')
        self.label3.config(text='spento')
        b1_c=self.button1.config('foreground')[-1]
        b2_c=self.button2.config('foreground')[-1]
        b3_c=self.button3.config('foreground')[-1]
        
        if b3_c=='green':
            if b2_c=='grey':
                self.button3.config(foreground='green', activeforeground='green')
                self.button2.config(foreground='yellow',activeforeground='yellow')
                self.label2.config(text='acceso')
                self.label3.config(text='acceso')
                self.button1.config(foreground='grey')
            else:
                self.button3.config(foreground='grey', activeforeground='grey')
                self.button2.config(foreground='grey', activeforeground='grey')
                self.button1.config(foreground='red',activeforeground='red')
                self.label2.config(text='spento')
                self.label1.config(text='acceso')
        else:
            self.button3.config(foreground='green', activeforeground='green')
            self.label3.config(text='acceso')
            self.button2.config(foreground='grey', activeforeground='grey')
            self.button1.config(foreground='grey', activeforeground='grey')
            
        

root = Tkinter.Tk()
app = MyApp(root)
root. mainloop()

No comments:

Post a Comment

How to create a virtual linux machine with qemu under Debian or Ubuntu with near native graphics performance

It's been a long time since my latest post. I know, I'm lazy. But every now and then I like to publish something that other people c...