[Tkinter-discuss] Filling a series of tk.Label

Michael O'Donnell michael.odonnell at uam.es
Tue Mar 17 21:33:32 CET 2009


Hi Madhu,

This is not in fact a Tkinter problem.

Try the following simplification of your code:

lbl = [[0]*2]*2
for i in range(2):
    for j in range(2):
        lbl[i][j] = str(i)+str(j)
        print ">", i, j, lbl[i][j], lbl
print lbl

------------------

The print statement within the loops shows that
every time you change the first row of the "array",
the other row changes as well.

Try substituting the first line for:

lbl=[[0,0], [0,0]]


Then no problem, the code works as expected.

So, I cannot explain why, but your way fo initialising the
'array' (in fact, a list of lists), seems to make a list of
lists where the lists are in fact the same list.
(I have never used '*' with lists, this may be the
problem.

Try instead an intialisation like:

x=[[0 for i in range(2)] for j in range(3)]

Mick





On Tue, Mar 17, 2009 at 8:44 PM, Madhu Subramaniam
<madhu.subramaniam at gmail.com> wrote:
> Iain,
>
> I made a small test program (Fairly new in Tkinter), i have not found the
> solution but i came across something, maybe somebody can throw more light on
> this ?
>
> Test Code (Active Python IDE 2.6 , Windows XP, Tkinter 8.4)
>
> import sys
> import pdb
> from Tkinter import *
> headers='ll'
> machines= 'zt'
> lbl = [[0]*len(headers)]*len(machines)
> flag=0
> root = Tk()
> for i in range(len(machines)):
>    for j in range(len(headers)):
>     lbl[i][j] = Label(root, text= str(i)+str(j), bg = 'red')
>     lbl[i][j].grid(row=i, column=j, padx=10, pady =5)
> print lbl # something that was noticed
> #print lbl[0][1]
> lbl[0][0].config(text= 'row=0, column=0') # changes the text in lbl [1] [0]
> lbl[0][1].config(text= 'row=0, column=1') # changes the text in lbl [1] [0]
> lbl[1][0].config(text= 'row=1, column=0')# overwrites the text in lbl [1]
> [0]
> lbl[1][1].config(text= 'row=1, column=1') # overwrites the text in lbl [1]
> [0]
> root.mainloop()
>
> OUTPUT
>>>> [[<Tkinter.Label instance at 0x012B3B70>, <Tkinter.Label instance at
>>>> 0x012B3AA8>], [<Tkinter.Label instance at 0x012B3B70>, <Tkinter.Label
>>>> instance at 0x012B3AA8>]]
>
> The instances generated in row wise are the same, but within a row they are
> different...
> something more can be learnt from that?? hmm, i got to leave now.
>
> Thanks
> Madhu
>
> On Tue, Mar 17, 2009 at 9:55 AM, Michael O'Donnell <michael.odonnell at uam.es>
> wrote:
>>
>> Iain,
>>
>>  You code is incomplete, the place where you create the Label widgets
>> is not included. We need that to see the problem.
>>
>> Check the grid operation on each Label, in particular the row parameter.
>>
>> Also, do a print as each Label is gridded, to see if they all are
>> actually created.
>>
>> Mick
>>
>> On Mon, Mar 16, 2009 at 10:50 PM, Iain Day
>> <iain at day-online.org.uk.invalid> wrote:
>> > Hi,
>> >
>> > I've written a short program which is supposed to populate a table
>> > constructed from tk.Label widgets. It does this by looping over the
>> > rows.
>> > Unfortunately, it only seems to populate the final row. What am I doing
>> > wrong?
>> >
>> > The code is:
>> >
>> > def updatedata():
>> >    updatetime.delete(0, tk.END)
>> >    for i in range(len(machines)):
>> >        print machines[i]
>> >        status = sshshowstat(machines[i])
>> >
>> >        for j in range(len(status)):
>> >            param, value = status[j].split(': ')
>> >
>> >            for k in range(len(variables)):
>> >                if re.match(variables[k], param) is None:
>> >                    continue
>> >                else:
>> > #                    print i, value
>> >                    if value in ('Acquiring', 'Regulated'):
>> >                        systemdata[i][k].config(text=value,
>> > foreground='green')
>> >                    elif value in ('Not Reg.'):
>> >                        systemdata[i][k].config(text=value,
>> > foreground='red')
>> >                    else:
>> >                        systemdata[i][k].config(text=value)
>> >
>> >
>> > tk.Button(root, text="Update Table Data",
>> > command=updatedata).grid(row=len(machines)+1, column=len(variables),
>> > sticky=tk.S)
>> >
>> >
>> >
>> > Thanks,
>> >
>> > Iain
>> >
>> > _______________________________________________
>> > Tkinter-discuss mailing list
>> > Tkinter-discuss at python.org
>> > http://mail.python.org/mailman/listinfo/tkinter-discuss
>> >
>> _______________________________________________
>> Tkinter-discuss mailing list
>> Tkinter-discuss at python.org
>> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
>


More information about the Tkinter-discuss mailing list