Sunday, February 9, 2014

Le Variable Classes in pratica

Il prossimo esempio dà applicazione concreta al concetto di variable class e tracing.
Vengono creati due frames, il primo ospita una Label e una Entry, il secondo cinque Radiobuttons.
Scopo del programma è acquisire da input una stringa, trattarla con i metodi di manipolazione standard di Python, restituirla nella Label modificata opportunamente come da scelta utente.
Le cinque modalità di trasformazione di una stringa sono:

  1. lower() -
  2. upper() -
  3. capitalize() -
  4. title() -
  5. swapcase() -

Come penso immaginiate, le possibilità di utilizzo di questo tipo di trasformazione di stringa sono molte e concrete, per esempio per dare un aspetto omogeneo a stringhe acquisite da un sito web o dal testo di un file.


 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
import Tkinter

class MyApp:

    def __init__(self, myProp):
        self.myProp = myProp
        self.myProp.title("Convertitore di stringhe")

        self.frame1 = Tkinter.Frame(self.myProp)
        self.frame1.pack()
        
        self.var1 = Tkinter.StringVar()
        self.var1.set('')
        self.var1.trace('w', self.change)
        self.entry1 = Tkinter.Entry(self.frame1, textvariable=self.var1,
                                    width = 40, font = 'Courier 24',
                                    relief='sunken'
                                    ).pack()
        self.var1.trace('w', self.change)
        
        self.var2 = Tkinter.StringVar()
        self.var2.set('')
        self.label1 = Tkinter.Label(self.frame1, textvariable=self.var2,
                                    width=40, font = 'Courier 24',
                                    relief='sunken'
                                    ).pack()

        
        self.frame2 = Tkinter.Frame(self.myProp)
        self.frame2.pack()

        self.var3 = Tkinter.IntVar()
        self.var3.set(1)
        self.radiobutton1 = Tkinter.Radiobutton(self.frame2, text="Tutto minuscolo",
                                                variable = self.var3, value=1).pack(side='left')
        self.radiobutton2 = Tkinter.Radiobutton(self.frame2, text="Tutto maiuscolo",
                                                variable = self.var3, value=2).pack(side='left')
        self.radiobutton3 = Tkinter.Radiobutton(self.frame2, text="Prima iniziale maiuscola",
                                                variable = self.var3, value=3).pack(side='left')
        self.radiobutton4 = Tkinter.Radiobutton(self.frame2, text="Ogni iniziale maiuscola",
                                                variable = self.var3, value=4).pack(side='left')
        self.radiobutton5 = Tkinter.Radiobutton(self.frame2, text="Inverti minusc/maiusc",
                                                variable = self.var3, value=5).pack(side='left')
        

    def change(self, *argv):
        scelta = self.var3.get()
        stringa = self.var1.get()
        if scelta == 1:
            self.var2.set(stringa.lower())
        elif scelta == 2:
            self.var2.set(stringa.upper())
        elif scelta == 3:
            self.var2.set(stringa.capitalize())
        elif scelta == 4: 
            self.var2.set(stringa.title())
        elif scelta == 5:
            self.var2.set(stringa.swapcase())
        else:
            pass

        

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

Quello che mi interessa far notare è che ogni volta che modifichiamo il testo nella Entry viene automaticamente lanciata la routine di trasformazione, che verifica quale delle opzioni è stata selezionata. Il semplice click sui radiobutton di per sè non lancia la routine di callback perchè non viene modificata la stringa.

Nel futuro sviluppo del nostro progetto, ricorreremo al tracing come modalità di ricalcolo automatico del tema astrologico ogni volta che una variabile temporale (data o ora) viene modificata.

Dal prossimo post inizieremo a parlare di Canvas, cioè di un widget ideato per disegnare in uno spazio bidimensionale dotato di coordinate.

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...