tkinter destroy()

max(01)* max2 at fisso.casa
Fri Apr 1 15:18:22 EST 2005


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

you are right, and at the end of this post you will find a new version 
of my code which heeds to your suggestion.

but i think there are exceptions.

consider for example building a BitmapImage for successive use in a 
widget, such as:

     self.immagine_1a = PhotoImage()
     self.immagine_1a.configure(
       file = "terra.gif"
       )

     self.e1 = Label(self.quadro_grande)
     self.e1.configure(
       image = self.immagine_1a,
       bd = 1,
       relief = SUNKEN
       )

if we follow your advice we should do:

     immagine_1a = PhotoImage()
     ....

instead, but it doesn't work. why?

> 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 thought about it. i prefer not to have lines longer than the usual 80 
char display, since (in my opinion) this produces an even more 
unreadable code layout. but we are starting a holy war i think...

> Also your three state
> variables could be members of a list, so you don't have to have
> separate constructors for each of them.
> 

it's true. but the drawback is that you have to keep an explicit 
mappping between names and variables; and what's worst is that this 
mapping must be known by the function that displays the second windows, 
and this is against data hiding.

if you are so kind as to peek at the attached code, you'll see that i 
also have restructured that part as one class. i'd like to hear your 
opinion about that. (now the code for the second window in completely 
reusable, i guess)

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

... and to use eval()


>         massimo = len(max(self.testo)) + 2

better:   massimo = max(map(len, self.testo)) + 2

....

anyway, here it is. bye!

----

from Tkinter import *

class MiaApp:
   def __init__(self, genitore):

     fonte = ("Helvetica", 12)

     quadro_grande = Frame(genitore)
     quadro_grande.pack(expand = YES, fill = BOTH)

     msg = Label(quadro_grande)
     msg.configure(
       font = 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.pack(side = TOP)

     pulsanti = Frame(quadro_grande)
     pulsanti.pack(side = BOTTOM, fill = X, padx = "2m")

     pulsanti_spunta = Frame(quadro_grande)
     pulsanti_spunta.pack(side = TOP, fill = X, padx = "2m")

     chiudi = Button(pulsanti)
     chiudi.configure(text = "Chiudi", command = genitore.destroy)
     chiudi.pack(side = LEFT, expand = YES)

     self.var = Button(pulsanti)
     self.var.configure(
       text = "Mostra Variabili",
       command = self.pulsanteMostraVariabiliPremuto,
       default = NORMAL
       )
     self.var.pack(side = LEFT, expand = YES)

     self.tergicristalli = IntVar()
     ps1 = Checkbutton(pulsanti_spunta)
     ps1.configure(
       text = "Tergicristalli a posto",
       variable = self.tergicristalli,
       relief = FLAT
       )
     ps1.pack(side = TOP, pady = 2, anchor = W)

     self.freni = IntVar()
     ps2 = Checkbutton(pulsanti_spunta)
     ps2.configure(
       text = "Freni a posto",
       variable = self.freni,
       relief = FLAT
       )
     ps2.pack(side = TOP, pady = 2, anchor = W)

     self.autista = IntVar()
     ps3 = Checkbutton(pulsanti_spunta)
     ps3.configure(
       text = "Autista sobrio",
       variable = self.autista,
       relief = FLAT
       )
     ps3.pack(side = TOP, pady = 2, anchor = W)

   def pulsanteMostraVariabiliPremuto(self):
     if self.var.cget("state") == ACTIVE:
       self.var.configure(state = DISABLED)
       mv = MostraVariabili(
	self,
	"tergicristalli",
	"freni",
	"autista"
	)

class MostraVariabili(Toplevel):
   def __init__(self, chiamante, *variabili):

     Toplevel.__init__(self)

     self.mioChiamante = chiamante

     fonteVar = ("Helvetica", 14)

     self.wm_title("Valori delle variabili")

     quadro_grande = Frame(self)
     quadro_grande.pack(expand = YES, fill = BOTH)

     titolo = Label(quadro_grande)
     titolo.configure(
       text = "Valori delle variabili:",
       width = 20,
       font = fonteVar
       )
     titolo.pack(side = TOP, fill = X)

     lung = max(map(len, variabili))

     dq = {}
     dn = {}
     dv = {}
     for i in variabili:
       dq[i] = Frame(quadro_grande)
       dq[i].pack(side = TOP, anchor = W, fill = X)

       dn[i] = Label(dq[i])
       dn[i].configure(
	text = i + ": ",
	width = lung + 2,
	anchor = W
	)
       dn[i].pack(side = LEFT)

       dv[i] = Label(dq[i])
       dv[i].configure(
	textvariable = eval("self.mioChiamante." + i),
	anchor = W
	)
       dv[i].pack(side = LEFT, expand = YES, fill = X)

     vaBene = Button(quadro_grande)
     vaBene.configure(
       text = "Va Bene",
       command = self.pulsanteVaBenePremuto,
       default = ACTIVE
       )
     vaBene.bind("<Return>", self.pulsanteVaBenePremuto_a)
     vaBene.focus_force()
     vaBene.pack(side = BOTTOM, pady = 2)

   def pulsanteVaBenePremuto(self):
     self.destroy()
     self.mioChiamante.var.configure(state = NORMAL)

   def pulsanteVaBenePremuto_a(self, evento):
     self.pulsanteVaBenePremuto()

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