problem with tkinter

Pierre Quentel quentel.pierre at wanadoo.fr
Tue Mar 29 15:32:59 EST 2005


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)

For your example, I wouldn't have used the "text" option in the 
definition of the labels, then "textvariable" in the callback method 
(procedi) ; I would have used only "text" and no IntVar or StringVar, 
just an integer and a string :

class MiaApp:
     def __init__(self, genitore):
        self.mioGenitore = genitore
        self.i = 42
        self.s = "Baobab"
        self.lab = {}
        self.lab[self.i] = Label(self.mioGenitore)
        self.lab[self.i].configure(width = 30, relief = RIDGE,
          text = "[vuota]")
        self.lab[self.i].pack()
        self.lab[self.s] = Label(self.mioGenitore)
        self.lab[self.s].configure(width = 30, relief = RIDGE,
          text = "[vuota]")
        self.lab[self.s].pack()
        self.but = Button(self.mioGenitore)
        self.but.configure(text = "Vai!", command = self.procedi)
        self.but.pack()
     def procedi(self):
        for var in (self.i, self.s):
          self.lab[var].configure(text = var)

Regards,
Pierre



More information about the Python-list mailing list