[Tkinter-discuss] Entry Labels - .get()

Michael Lange klappnase at web.de
Fri Feb 1 10:52:36 CET 2013


Hi,

On Thu, 31 Jan 2013 15:12:19 -0800 (PST)
Blackpanda <darragh_odowd at yahoo.com> wrote:

> but this only gives me the last entry. Any suggestions how I can get
> all the entries when the button is pressed?

as Lion pointed out, your problem here is that the references to the
widgets and variables are overridden on each iteration of the for-loop so
only the last reference is actually stored. A slightly different approach
to solve your problem than the one mentioned by Lion might look like the
following snippet for which I modified your example a little:

        #Frame contents
        self.vars = []
        for i in range(0, len(Assets_Text)):
            Label(frm2, width=30, text=Assets_Text[i], anchor='w').grid(
                                      row=i+5, column=0, sticky='we')

            for j in range(0, self.entry2Variable.get()):
                Label(frm2, text="Portfolio %d" % (j+1), width=10,
                             font=("Calibri", 10)).grid(row=4, column=j+1)
                entVar = IntVar(value=0)
                self.vars.append(entVar)
                Entry(frm2, width=10, textvariable=entVar).grid(
                                                  row=i+5, column=j+1)

You see, since the references to the vars and widgets are lost with each
iteration of the loop anyway, we can spare them at all which saves a few
lines; the vars are instead dynamically appended to a list.
BTW, in your example you set the initial value of the IntVar to 0.00
which is most likely no good idea. If you need to handle float values you
best use DoubleVar objects instead.

I hope this helps

Michael

.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Killing is stupid; useless!
		-- McCoy, "A Private Little War", stardate 4211.8


More information about the Tkinter-discuss mailing list