Why widgets become 'NoneType'?

2011-12-21 Thread Muddy Coder
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why widgets become 'NoneType'?

2011-12-21 Thread Dave Angel

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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Why widgets become 'NoneType'?

2011-12-21 Thread woooee
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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why widgets become 'NoneType'?

2011-12-21 Thread Rick Johnson
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'
-- 
http://mail.python.org/mailman/listinfo/python-list