Why widgets become 'NoneType'?

M

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).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
 
D

Dave Angel

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).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.
 
W

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).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.
 
R

Rick Johnson

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).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 'NoneType'>
#
# Now we separate the packing from the instancing.
#
label = Label(root)
label.pack()
type(label)
label['bg']
'SystemButtonFace'

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top