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
hi pierre.
i don't think this would not have worked as expected (by me). in my intentions, the text of the label must be slaved to a variable, so that it would change dynamically during the mainloop execution if another part of the code had chenged the content of the variable.
maybe here is a more convincing example (the previous one was contrived too hastily i guess):
1 from Tkinter import *
2
3 class MiaApp:
4 def __init__(self, genitore):
5 self.mioGenitore = genitore
6 self.var = {0: 42, 1: "Baobab"}
7 self.lab = {}
8 self.lab[0] = Label(self.mioGenitore)
9 self.lab[0].configure(width = 30, relief = RIDGE,
10 text = "[vuota]")
11 self.lab[0].pack()
12 self.lab[1] = Label(self.mioGenitore)
13 self.lab[1].configure(width = 30, relief = RIDGE,
14 text = "[vuota]")
15 self.lab[1].pack()
16 self.but = Button(self.mioGenitore)
17 self.but.configure(text = "Vai!", command = self.procedi)
18 self.but.pack()
19 self.but2 = Button(self.mioGenitore)
20 self.but2.configure(text = "Torna!", command = self.procedi2)
21 self.but2.pack()
22 def procedi(self):
23 for var in self.lab.keys():
24 self.lab[var].configure(text = self.var[var])
25 def procedi2(self):
26 self.var[0] = 24
27 self.var[1] = "Cactus"
28
29 radice = Tk()
30 miaApp = MiaApp(radice)
31 radice.mainloop()
in this example, when user presses "Torna!", the labels are not updated as i expect; they only will be when user presses "Vai!" again (not what i want).
thanks again
macs -- http://mail.python.org/mailman/listinfo/python-list