Why widgets become 'NoneType'?

Muddy Coder cosmo_general at yahoo.com
Wed Dec 21 07:59:59 EST 2011


Hi Folks,

I was driven nuts by this thing: widgets lost their attributes, then I
can't configure them. Please take a look at the codes below:

from Tkinter import *
canvas = Canvas(width=300, height=400, bg='white')
canvas.pack(expand=NO, fill=BOTH)
pic = PhotoImage(file=img)
canvas.create_image(0, 0, image=pic)
al = PhotoImage(file='a.gif')

lbl = Label(canvas, text=x, fg='red').pack(side=LEFT)

canvas.create_text(40,20, text='Howdy')

the last line of code got error message, says canvas object has no
attribute of config.  The same problem happened again, the code is
below:

def make_labels(win, astr):

    labels = []
    for i in range(len(astr)):
        lbl = Label(win, text=astr[i]).pack(side=LEFT )
        labels.append(lbl)
    return tuple(labels)
def config_labels(atuple, func):
    for lbl in atuple:
        lbl.config('%s') % func

root = Tk()
lbls = make_labels(root, 'foobar')
config_labels(lbls, "fg='red'")

The config for Label was picked up: No attributes of config. I tried
to print type(lbls), and found Python interpreter reported as
'NoneType'. I also tried to print out dir(lbls), fount there was no
attributes I familiar with such as config and such. What is wrong with
them? Both of the codes above WORKED, since I saw my widgets displayed
them as I wanted, but I just can't configure them as I usually did.
Can somebody help me out? I never experience such a weird thing.
Thanks!

Cosmo



More information about the Python-list mailing list