Eric Brunel wrote:
On Tue, 29 Mar 2005 22:32:59 +0200, Pierre Quentel
<qu************ @wanadoo.fr> wrote:
Instead of indexing self.lab by strings, you can index them by the
attributes themselves : self.lab[self.i], and change line 23 into
for var in (self.s, self,i)
I really think this is asking for trouble: I suppose that the i and s
attributes are meant to change at some point in the future, and you're
mapping their *values* to the corresponding labels. So if the value
changes, you won't be able to get the label again.
For your example, I wouldn't have used the "text" option in the
definition of the labels, then "textvariab le" in the callback method
(procedi) ; I would have used only "text" and no IntVar or StringVar,
just an integer and a string :
I would have done exactly the contrary, as it is far more easier to use.
Using the textvariable option in Label's does not require you to
remember the labels, but only the variables used to hold their contents.
I'd also use two mappings: the first mapping a name to a Tkinter
variable for the label variables, and the second for the values to give
to these variables later.
Here is my version of the class code:
class MiaApp:
def __init__(self, genitore):
self.mioGenitor e = genitore
## Mapping for variables
self.variables = {
"i" : StringVar(),
"s" : StringVar()
}
## Mapping for future variable values
self.values = {
"i" : 42,
"s" : "Baobab"
}
## Now, create the labels
for var in self.variables. values():
## Default text
var.set("[vuota]")
## Create label
lb = Label(self.mioG enitore, width=30, relief=RIDGE,
textvariable=va r)
lb.pack()
## The button is no more remembered in an attribute, as it does
not seem to be needed
but = Button(self.mio Genitore, text = "Vai!", command =
self.procedi)
but.pack()
def procedi(self):
## Just change the variable values
for varName in self.variables. keys():
self.variables[varName].set(self.value s[varName])
your technique is most interirting and clean, i must say.
nevertheless, i cannot find a natural way to "modularize " it, in such a
way that the machinery of the method might be isolated from the code
that uses it.
consider for example the following two programs:
.....
$ cat Miodialogo.py
from Tkinter import *
class MioDialogo(Topl evel):
def __init__(self, genitore, chiamante):
Toplevel.__init __(self, genitore)
self.wm_title(" Valori delle variabili")
self.mioGenitor e = genitore
self.mioChiaman te = chiamante
self.fonteVar = ("Helvetica" , 14)
self.quadro_gra nde = Frame(self)
self.quadro_gra nde.pack(expand = YES, fill = BOTH)
self.titolo = Label(self.quad ro_grande)
self.titolo.con figure(
text = "Valori delle variabili:",
width = 20,
font = self.fonteVar
)
self.titolo.pac k(side = TOP, fill = X)
def mostraVariabili (self, *argomenti):
lung = 1
for i in argomenti:
if len(i) > lung:
lung = len(i)
self.dq = {}
self.dn = {}
self.dv = {}
for i in argomenti:
self.dq[i] = Frame(self.quad ro_grande)
self.dq[i].pack(
side = TOP,
anchor = W,
fill = X
)
self.dn[i] = Label(self.dq[i])
self.dn[i].configure(
text = i + ": ",
width = lung + 2,
anchor = W
)
self.dn[i].pack(
side = LEFT
)
self.dv[i] = Label(self.dq[i])
self.dv[i].configure(
textvariable = eval("self.mioC hiamante." + i),
anchor = W
)
self.dv[i].pack(
side = LEFT,
expand = YES,
fill = X
)
self.vaBene = Button(self.qua dro_grande)
self.vaBene.con figure(
text = "Va Bene",
command = self.pulsanteVa BenePremuto,
default = ACTIVE
)
self.vaBene.bin d(
"<Return>",
self.pulsanteVa BenePremuto_a
)
self.vaBene.foc us_force()
self.vaBene.pac k(
side = BOTTOM,
pady = 2
)
def pulsanteVaBeneP remuto(self):
self.destroy()
self.mioChiaman te.var.configur e(state = NORMAL)
def pulsanteVaBeneP remuto_a(self, evento):
self.pulsanteVa BenePremuto()
$ cat spunta-4.py
from Tkinter import *
from Miodialogo import *
class MiaApp:
def __init__(self, genitore):
self.mioGenitor e = genitore
self.fonte = ("Helvetica" , 12)
self.quadro_gra nde = Frame(genitore)
self.quadro_gra nde.pack(expand = YES, fill = BOTH)
self.msg = Label(self.quad ro_grande)
self.msg.config ure(
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."
)
self.msg.pack(s ide = TOP)
self.pulsanti = Frame(self.quad ro_grande)
self.pulsanti.p ack(side = BOTTOM, fill = X, padx = "2m")
self.pulsanti_s punta = Frame(self.quad ro_grande)
self.pulsanti_s punta.pack(side = TOP, fill = X, padx = "2m")
self.annulla = Button(self.pul santi)
self.annulla.co nfigure(
text = "Annulla",
command = self.mioGenitor e.destroy
)
self.annulla.pa ck(side = LEFT, expand = YES)
self.var = Button(self.pul santi)
self.var.config ure(
text = "Mostra Variabili",
command = self.pulsanteMo straVariabiliPr emuto,
default = NORMAL
)
self.var.pack(s ide = LEFT, expand = YES)
self.tergicrist alli = IntVar()
self.b1 = Checkbutton(sel f.pulsanti_spun ta)
self.b1.configu re(
text = "Tergicrist alli a posto",
variable = self.tergicrist alli,
relief = FLAT
)
self.b1.pack(
side = TOP,
pady = 2,
anchor = W
)
self.freni = IntVar()
self.b2 = Checkbutton(sel f.pulsanti_spun ta)
self.b2.configu re(
text = "Freni a posto",
variable = self.freni,
relief = FLAT
)
self.b2.pack(
side = TOP,
pady = 2,
anchor = W
)
self.autista = IntVar()
self.b3 = Checkbutton(sel f.pulsanti_spun ta)
self.b3.configu re(
text = "Autista sobrio",
variable = self.autista,
relief = FLAT
)
self.b3.pack(
side = TOP,
pady = 2,
anchor = W
)
def pulsanteMostraV ariabiliPremuto (self):
if self.var.cget(" state") == ACTIVE:
self.var.config ure(state = DISABLED)
self.dialogo = MioDialogo(self .mioGenitore, self)
self.dialogo.mo straVariabili(" tergicristalli" ,
"freni",
"autista")
radice = Tk()
radice.wm_title ("Dimostrazi one Pulsanti a Spunta")
radice.wm_iconn ame("spunta")
miaApp = MiaApp(radice)
radice.mainloop ()
.....
the program spunta-4.py uses the module Miodialogo.py, which can be
usedby any other program simply passing along strings containing the
names of the attributes to be displayed.
i cannot find a similar way to modularize your technique (maybe it is my
fault).
thanks again
macs