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


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

Global NameError Fix?

Started bymaiden129 <sengokubasarafever@gmail.com>
First post2013-03-21 16:43 -0700
Last post2013-03-21 21:04 -0400
Articles 4 — 4 participants

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


Contents

  Global NameError Fix? maiden129 <sengokubasarafever@gmail.com> - 2013-03-21 16:43 -0700
    Re: Global NameError Fix? Dave Angel <davea@davea.name> - 2013-03-21 20:24 -0400
      Re: Global NameError Fix? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-03-22 10:21 -0700
    Re: Global NameError Fix? David Robinow <drobinow@gmail.com> - 2013-03-21 21:04 -0400

#41679 — Global NameError Fix?

Frommaiden129 <sengokubasarafever@gmail.com>
Date2013-03-21 16:43 -0700
SubjectGlobal NameError Fix?
Message-ID<44451203-05be-4336-9325-c956b0ee2116@googlegroups.com>
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()

[toc] | [next] | [standalone]


#41681

FromDave Angel <davea@davea.name>
Date2013-03-21 20:24 -0400
Message-ID<mailman.3608.1363911877.2939.python-list@python.org>
In reply to#41679
On 03/21/2013 07:43 PM, maiden129 wrote:
> 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.


>
> window = Tk()
> window.title("counter")
>
>
> counterButton1 = CounterButton(window)
> counterButton2 = CounterButton(window)
>
>
> counterButton1.pack()
> counterButton1.pack()
>
>
> window.mainloop()
>


-- 
DaveA

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


#41693

FromRick Johnson <rantingrickjohnson@gmail.com>
Date2013-03-22 10:21 -0700
Message-ID<d59f03c6-5b0f-480d-8030-dd990a96c727@googlegroups.com>
In reply to#41681
On Thursday, March 21, 2013 7:24:17 PM UTC-5, Dave Angel wrote:
> On 03/21/2013 07:43 PM, maiden129 wrote:
> > 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
> 
> [...]
> 
> 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 by the 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*

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


#41682

FromDavid Robinow <drobinow@gmail.com>
Date2013-03-21 21:04 -0400
Message-ID<mailman.3609.1363914257.2939.python-list@python.org>
In reply to#41679
On Thu, Mar 21, 2013 at 7:43 PM, maiden129 <sengokubasarafever@gmail.com> wrote:
> 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")
...

[toc] | [prev] | [standalone]


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


csiph-web