Why widgets become 'NoneType'?

Rick Johnson rantingrickjohnson at gmail.com
Wed Dec 21 22:43:43 EST 2011


On Dec 21, 6:59 am, Muddy Coder <cosmo_gene... at yahoo.com> wrote:
> 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:


The problem is here...

>     labels = []
>     for i in range(len(astr)):
>         lbl = Label(win, text=astr[i]).pack(side=LEFT )

label.pack() returns None. So you are creating a variable named "lbl"
the value of which is None. Don't believe me? Try this...


>>> from Tkinter import *
>>> root = Tk()
>>> label = Label(root).pack()
>>> label['bg']
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    label['bg']
TypeError: 'NoneType' object is not subscriptable
>>> type(label)
<type 'NoneType'>
#
# Now we separate the packing from the instancing.
#
>>> label = Label(root)
>>> label.pack()
>>> type(label)
<type 'instance'>
>>> label['bg']
'SystemButtonFace'

Same with list.sort.

>>> lst = range(5)
>>> lst
[0, 1, 2, 3, 4]
>>> var = lst.sort()
>>> repr(var)
'None'



More information about the Python-list mailing list