[Tutor] NameError

Wayne srilyk at gmail.com
Mon Sep 21 23:06:43 CEST 2009


On Mon, Sep 21, 2009 at 4:00 PM, kreglet <kreglet at gmail.com> wrote:

>
> I keep getting the following error and don't uderstand why:
>
> Traceback (most recent call last):
>  File "/home/kreglet/bin/test.py", line 15, in btnStatclick
>    btnStat.set_label("Pressed")
> NameError: global name 'btnStat' is not defined
>
>
> #!/usr/bin/env python
>
> import gtk
> import sys
>
> class NewAppWindow(gtk.Window):
>
>    def btnStatclick(self, widget, data=None):
>        #print status
>        if self.status == True:
>            btnStat.set_label("Not Pressed")
>            self.status =False
>            print self.status
>        elif self.status == False:
>            btnStat.set_label("Pressed")
>            self.status =True
>            print self.status


btnStat isn't defined here. I think you should be declaring a self.btnStat
in the __init__ method (which I usually put as the first function in my
class - I'm honestly not sure if it makes a difference), and calling
self.btnStat

Unlike C/C++, python is explicit when dealing with members of the class. I'm
not sure if anything is implied when it comes to namespaces, but I'm pretty
sure it's all gotta be specific.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090921/0c259b15/attachment.htm>


More information about the Tutor mailing list