Global NameError Fix?

M

maiden129

Hello,

I'm using the version 3.2.3 of Python and I am having an issue in my program and I don't know how to fix it:

counterLabel["text"] = str(counter)
NameError: global name 'counterLabel' is not defined

Here is my program:

from tkinter import *


class CounterButton(Button):

def __init__(self, window):


super(CounterButton,self).__init__(window, text = "0", command=self.startCounter)


def startCounter(self):
counter = int(self["text"])
counter +=1
counterLabel["text"] = str(counter)

window = Tk()
window.title("counter")


counterButton1 = CounterButton(window)
counterButton2 = CounterButton(window)


counterButton1.pack()
counterButton1.pack()


window.mainloop()
 
D

Dave Angel

Hello,

I'm using the version 3.2.3 of Python and I am having an issue in my program and I don't know how to fix it:

counterLabel["text"] = str(counter)
NameError: global name 'counterLabel' is not defined

Please include the entire traceback when reporting an exception. In
this case, we can figure it out, but frequently we can't.
Here is my program:

from tkinter import *


class CounterButton(Button):

def __init__(self, window):


super(CounterButton,self).__init__(window, text = "0", command=self.startCounter)


def startCounter(self):
counter = int(self["text"])
counter +=1
counterLabel["text"] = str(counter)

Where did you think counterLabel was defined? It needs to be a dict or
equivalent, in order for you to use the ["text"] notation on it. If
it's supposed to be an attribute of the CounterButton, then you should
create it in the __init__() method, as

self.counterLabel = {}

And if it's supposed to be inherited from Button (unlikely, with that
name), presumably it's initialized there.

In either case, if it's supposed to be specific to this instance of
CounterButton, you need the self. prefix:

self.counterLabel["text"] = ...

I don't use tkinter, and it's not installed in my Python, but I suspect
that it is in the Button object, and it's called something else, like text.

Based on a quick internet search, I might try something like:

self.config(text= str(counter))


This is assuming you actually wanted to change the text on the button
itself. You may well want to change something else in your GUI.
 
D

David Robinow

Hello,

I'm using the version 3.2.3 of Python and I am having an issue in my program and I don't know how to fix it:

counterLabel["text"] = str(counter)
NameError: global name 'counterLabel' is not defined

Here is my program:

from tkinter import *
class CounterButton(Button):
def __init__(self, window):
super(CounterButton,self).__init__(window, text = "0", command=self.startCounter)

def startCounter(self):
counter = int(self["text"])
counter +=1
counterLabel["text"] = str(counter)
This should be:
self["text"] = str(counter)
window = Tk()
window.title("counter")
....
 
R

Rick Johnson

I'm using the version 3.2.3 of Python and I am having an
issue in my program and I don't know how to fix it:
counterLabel["text"] = str(counter)
NameError: global name 'counterLabel' is not defined

[...]

Where did you think counterLabel was defined? It needs to
be a dict or equivalent, in order for you to use the
["text"] notation on it. If it's supposed to be an
attribute of the CounterButton, then you should create it
in the __init__() method, as

self.counterLabel = {}

The decision by the Tkinter designer to allow the syntactic sugar of "__setitem__" for configuring a Tkinter widget is unfortunate. And it's not unfortunate /solely/ because of it's detrimental propagation of a well established "sugar" for "list indexing" and "dict key resolution", but compounded bythe fact that misunderstands will abound from having more than one way (and preferably only one way!) of doing something.

widget.configure(key=value)
widget['key'] = value

....and then, when you thought the API could not get any worse, they went and created an alias for "configure":

widget.config(key=value)

....are three tiny little letters really enough to justify injecting multiplicity into your API? REALLY? Same foolishness goes for fetching values:

value = widget['key']
value = widget.cget(key)

And whilst they spared us the multiplicity of a short and long form, their choice of "cget" does not play well with "configure" or "config".

Here's a free tutorial for all you "API designers" out there: If you want to be concise, whilst simultaneously maintain consistency between getters and setters, and you're an enemy of multiplicity, then you would want to create this API:

value = widget.cget(key)
widget.cset(key=value)

.... where "cget" is short for "configuration_get" and "cset" is short for....oh gawd! DO I REALLY NEED TO TYPE THIS OUT FOR YOU PEOPLE!!!

*school-bell*
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top