Tuesday, February 11, 2014

I widget di Tkinter. Metodi comuni.

Tutti i widget istanziabili in Tkinter possiedono alcuni metodi base, utilizzabili per definire o modificare il comportamento base del widget stesso.

Abbiamo già incontrato il metodo config o configure che serve a settare le variabili interne del widget, i colori, le variabili, la grandezza, il binding e, come abbiamo visto nella piccola applicazione che creava un semaforo simulato, permette anche di recuperare le informazioni di settaggio, se richiesto senza parametri, restituendo una tupla.

Un metodo più rapido è cget:

widget.cget('opzione').

L'uso di cget, eventualmente combinata con config, permette di ricavare i valori di istanza associati ad un determinato widget.

Altro metodo che abbiamo già incontrato è mainloop, che mantiene in ciclo indefinito l'istanza di Tk() mantenendo l'applicazione in uno stato di ascolto di eventi.

Un metodo senza dubbio indispensabile è .after, che consente la temporizzazione di chiamate a funzioni di callback dopo un numero predefinito di millisecondi. Questo metodo fornisce un timer che svincola il programma da interventi dell'utente, come gli eventi del mouse o della tastiera e provvede quindi una modalità di animazione del software utile per scandire cambiamenti di stato delle variabili o altro tipo di segnali.

Per darvi modo di apprezzare l'uso di questi metodi ho riscritto l'applicazione 'semaforo' in modo da includerne alcuni. In particolare il metodo after, che temporizza la durata del segnale luminoso, avviando quindi il ciclo, e l'istruzione cget per il recupero della variabile [foreground] del widget button. Ho pensato anche di migliorare la classe che crea l'applicazione, spostando all'interno della class la creazione dell'istanza di Tk e definendo un metodo apposito per il mainloop.


 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
86
# -*- coding: utf-8 -*-
import Tkinter

class MyApp:

    def __init__(self):
        
        self.root = Tkinter.Tk()
        self.frame1 = Tkinter.Frame()
        self.frame1.pack()
        
        self.button1 = Tkinter.Button(self.frame1,font="Courier 36",
                                      text=u"\u25cf",
                                      foreground="grey",
                                      activeforeground="grey",
                                      background="grey",
                                      )
        self.button1.pack(side='left')
        self.label1 = Tkinter.Label(self.frame1, text="spento")
        self.label1.pack(side='left')

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

        self.frame3 = Tkinter.Frame()
        self.frame3.pack()
        self.button3 = Tkinter.Button(self.frame3,font="Courier 36",
                                      text=u"\u25cf",
                                      foreground="green",
                                      activeforeground="green",
                                      background="grey",
                                      )
        self.button3.pack(side='left')
        self.label3 = Tkinter.Label(self.frame3, text="acceso")
        self.label3.pack(side='left')

        self.root.after(5000,self.change)

    def mainloop(self):
        self.root.mainloop()

 
    def change(self):

        self.label1.config(text='spento')
        self.label2.config(text='spento')
        self.label3.config(text='spento')
        b1_c=self.button1.cget('foreground')
        b2_c=self.button2.cget('foreground')
        b3_c=self.button3.cget('foreground')
        
        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')
                self.root.after(2000,self.change)
                
            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')
                self.root.after(5000,self.change)
        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')
            self.root.after(5000,self.change)


app = MyApp()
app.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...