Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17657 > unrolled thread

Why widgets become 'NoneType'?

Started byMuddy Coder <cosmo_general@yahoo.com>
First post2011-12-21 04:59 -0800
Last post2011-12-21 19:43 -0800
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Why widgets become 'NoneType'? Muddy Coder <cosmo_general@yahoo.com> - 2011-12-21 04:59 -0800
    Re: Why widgets become 'NoneType'? Dave Angel <d@davea.name> - 2011-12-21 08:22 -0500
    Re: Why widgets become 'NoneType'? woooee <woooee@gmail.com> - 2011-12-21 09:45 -0800
    Re: Why widgets become 'NoneType'? Rick Johnson <rantingrickjohnson@gmail.com> - 2011-12-21 19:43 -0800

#17657 — Why widgets become 'NoneType'?

FromMuddy Coder <cosmo_general@yahoo.com>
Date2011-12-21 04:59 -0800
SubjectWhy widgets become 'NoneType'?
Message-ID<2073cd5d-a48e-4fa3-8f68-3becbfd8ec97@o7g2000yqk.googlegroups.com>
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

[toc] | [next] | [standalone]


#17660

FromDave Angel <d@davea.name>
Date2011-12-21 08:22 -0500
Message-ID<mailman.3912.1324473733.27778.python-list@python.org>
In reply to#17657
On 12/21/2011 07:59 AM, Muddy Coder 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:
>
> 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
When posting error messages, post it exactly and completely;  do not 
paraphrase.

-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#17680

Fromwoooee <woooee@gmail.com>
Date2011-12-21 09:45 -0800
Message-ID<d7c66ab0-08a8-4aaf-9f74-9d565d905f51@24g2000yqi.googlegroups.com>
In reply to#17657
The error is with the labels not the canvas.  All labels will have an
id of "None" as that is what pack() returns.
         lbl = Label(win, text=astr[i]).pack(side=LEFT )
         labels.append(lbl)
The error will come up in the config_labels function when the program
tries to config a tuple of "None", if not before.  Other than that,
the previous suggestion, post the entire error message so we know
where an what is happening, is required to debug further.

[toc] | [prev] | [next] | [standalone]


#17714

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2011-12-21 19:43 -0800
Message-ID<118d4919-6df3-457d-9409-d95a45b989ec@k10g2000yqk.googlegroups.com>
In reply to#17657
On Dec 21, 6:59 am, Muddy Coder <cosmo_gene...@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'

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web