tkinter destroy()

stewart.midwinter at gmail.com stewart.midwinter at gmail.com
Thu Mar 31 02:47:21 EST 2005


Your app seems to give the right state values only if you select 'Freni
a posto'.   But I see you recognize that with your 'FIXME' note.

also the app seems to have too many variables and widgets defined as
self objects.  That isn't necessary unless they will be used outside
the method they were created in (which labels and buttons usually
aren't), so all you are doing is using up more memory than necessary.


Reading the code with Italian names adds a little difficulty in
understanding your code (non parlo italiano ma si parlo espagnol), but
I'm left feeling that your app is more complicated than it needs to be
- unless I'm missing something.  What you are doing is just showing how
you can capture the state of the checkbuttons for use elsewhere, right?
And also, that the state in the 2nd window should be live, so that it
updates with the change in value in the 1st window?   And just a matter
of personal taste, but splitting up widget configuration over many
lines for me impedes readiblity and makes the code look like java or
c++ : surely not what we want?

I also think you could get away with no frames in your initial window,
at least if you use grid() instead of pack().  Also your three state
variables could be members of a list, so you don't have to have
separate constructors for each of them.

Anyway here's a version of your app that makes use of a 'for' statement
to draw the labels and checkbuttons, so it's only half as long as your
original app.  It also does the right thing - the key pont you were
missing was to use a 'textvariable' argument in defining your label
widgets in the 2nd window.

cheers
Stewart in Calgary

---
#tested on Windows XP with Python 2.4

from Tkinter import *

class MiaApp:
    def __init__(self, genitore):
        self.debug = 1  #debug flag
        self.fonte = ("Helvetica", 12)
        self.fonteVar = ("Helvetica", 14)

        msg = Label(genitore, font = self.fonte, wraplength = "10c",
                justify = LEFT,
                text = u"Sono qui sotto presentati tre pulsanti a
spunta. \
Premendo un pulsante, se ne varia lo stato di selezione e si \
imposta una variabile a un valore che indica lo stato del \
pulsante stesso. Premendo il pulsante \u00ABMostra \
Variabili\u00BB si possono vedere i valori correnti delle \
variabili.")
        msg.grid(row=0, column=0, sticky='w')

        testo = ["Tergicristalli a posto", "Freni a posto", "Autista
sobrio"]
        self.testo = testo
        variabili = []
        pulsanti  = []
        for i in range(0,3):
            variabili.append(None)
            variabili[i] = IntVar()
            variabili[i].set(0)

            pulsanti.append(None)
            pulsanti[i] = Checkbutton(genitore, text = testo[i],
                variable = variabili[i], relief = FLAT)
            pulsanti[i].grid(row=i+1, column=0,pady = 2,sticky = 'w')

        self.variabili = variabili

        var1 = Button(genitore, text = "Mostra Variabili",
                command = self.pulsanteMostraVariabiliPremuto)
        var1.grid(row=4, column=0)

        annulla = Button(genitore, text = "Annulla",
                    command = genitore.destroy)
        annulla.grid(row=4, column=1)


    def pulsanteMostraVariabiliPremuto(self):
        argomenti = []
        for i in range(0,len(self.variabili)):
            argomenti.append(self.variabili[i].get())
            if self.debug:
                print "%s=%i" % (self.testo[i],
self.variabili[i].get())
        self.mostraVariabili(argomenti)


    def mostraVariabili(self, *argomenti):
        dialogo = Toplevel()
        dialogo.wm_title("Valori delle variabili")
        self.dialogo = dialogo

        titolo = Label(dialogo,
            text = "Valori delle variabili:", width = 20,
            font = self.fonteVar )
        titolo.grid(row=0, column=0, sticky='w' )

        massimo = len(max(self.testo)) + 2
        valore   = []
        pulsanti = []
        for i in range(0,3):
            pulsanti.append(None)

            pulsanti[i] = Label(dialogo,
                    text = 'self.%s' % self.testo[i].split()[0],
                    width = massimo)
            pulsanti[i].grid(row=1+i, column=0, pady = 2,sticky = 'w')
            valore.append(None)
            valore[i] = Label(dialogo, text = self.variabili[i].get(),
                textvariable = self.variabili[i])
            valore[i].grid(row=i+1, column=1, pady = 2, sticky = 'w')

        vaBene = Button(dialogo, text = "Va Bene",
                    command = self.pulsanteVaBenePremuto)
        vaBene.bind("<Return>", lambda e=1:
self.pulsanteVaBenePremuto() )
        vaBene.focus_force()
        #items can span more than one column if desired
        vaBene.grid(row=4, column=0, pady = 2, sticky='e')

    def pulsanteVaBenePremuto(self):
        self.dialogo.destroy()


radice = Tk()
radice.wm_title("Dimostrazione Pulsanti a Spunta")
radice.wm_iconname("spunta")
miaApp = MiaApp(radice)
radice.mainloop()




More information about the Python-list mailing list