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


Groups > comp.lang.python > #41681

Re: Global NameError Fix?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'attribute': 0.05; 'exception.': 0.07; 'method,': 0.07; 'tkinter': 0.07; 'python': 0.09; 'dict': 0.09; 'inherited': 0.09; 'nameerror:': 0.09; 'notation': 0.09; 'def': 0.10; 'itself.': 0.11; "can't.": 0.16; 'name),': 0.16; 'prefix:': 0.16; 'tk()': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'else,': 0.17; 'instance': 0.17; 'import': 0.21; 'supposed': 0.21; 'assuming': 0.22; 'defined': 0.22; 'installed': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'object,': 0.27; 'there.': 0.28; 'equivalent,': 0.29; 'initialized': 0.29; 'case,': 0.29; 'reporting': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'window': 0.30; 'button': 0.30; 'figure': 0.30; 'like:': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; 'text': 0.34; 'text.': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'something': 0.35; 'but': 0.36; 'wanted': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'hello,': 0.39; 'called': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'think': 0.40; 'your': 0.60; 'here': 0.65; 'frequently': 0.65; 'search,': 0.65; 'internet': 0.71; 'received:74.208': 0.71; 'presumably': 0.84; 'received:74.208.4.194': 0.84; 'subject:Global': 0.93
Date Thu, 21 Mar 2013 20:24:17 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4
MIME-Version 1.0
To python-list@python.org
Subject Re: Global NameError Fix?
References <44451203-05be-4336-9325-c956b0ee2116@googlegroups.com>
In-Reply-To <44451203-05be-4336-9325-c956b0ee2116@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:7IHJthwVIpUgwY75InqXPQt8huyIQXg08moejQrQb3Z x7tf9hNAMIIUaU/FnYP65TLWZfiiOqx+9/dNdbByAM2zmd87I3 Wvbqxx9difmGA+hr/nmuh6n1RO1F5nNYHoeXInFa6YtIB98iCG WWsHAM9Bo9U++kRUcmAz2BKSUXsaxp3k2gsINyRtUQe4aOr1cH RvCEAMfdQ2QUUTV0N0tz9Kn8WaUcF2vdzxHoKF28y/e5dcJAG/ X2m2pk7CgVEGgVwEB10ZpU3Hyl1wFuC62sNFUD+gjYc2IMnJ3I 1ptkbMyAg3ydZ32lvcuP6VBKrzabIeffuiI4TK+eFlWymWMNQ= =
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3608.1363911877.2939.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363911877 news.xs4all.nl 6843 [2001:888:2000:d::a6]:49791
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41681

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web