Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > confusion between global names and instantiated object variable names

Reply
Thread Tools

confusion between global names and instantiated object variable names

 
 
wanwan
Guest
Posts: n/a
 
      10-14-2005
I'm trying to make a GUI, but for some of the instantiated object
variable names, the interpreter is looking at them as global names.
Here is an example of what I did:


class mygui:


def __init__(self, root):

self.menubar = Menu(root)

# Game Menu
self.menu1 = Menu(self.menubar, tearoff=0)
self.menu1.add_command(label="Open File", command=donothing)
self.menu1.add_separator()
self.menu1.add_command(label="Exit", command=root.quit)
self.menubar.add_cascade(label="File", menu=self.menu1)

# ignoring the rest of the program ...


when I run my example, an error shows:
"NameError: global name'menubar' is not defined"

I wonder why it doesn't work. Isn't that the way to define an object
variable?

Any response would be appreciated.

 
Reply With Quote
 
 
 
 
Piet van Oostrum
Guest
Posts: n/a
 
      10-14-2005
>>>>> "wanwan" <(E-Mail Removed)> (w) wrote:

>w> I'm trying to make a GUI, but for some of the instantiated object
>w> variable names, the interpreter is looking at them as global names.
>w> Here is an example of what I did:



>w> class mygui:



>w> def __init__(self, root):


>w> self.menubar = Menu(root)


>w> # Game Menu
>w> self.menu1 = Menu(self.menubar, tearoff=0)
>w> self.menu1.add_command(label="Open File", command=donothing)
>w> self.menu1.add_separator()
>w> self.menu1.add_command(label="Exit", command=root.quit)
>w> self.menubar.add_cascade(label="File", menu=self.menu1)


>w> # ignoring the rest of the program ...



>w> when I run my example, an error shows:
>w> "NameError: global name'menubar' is not defined"


If it talks about global name, it can't be self.menubar or
anything.menubar. So there must be a soloist menubar reference somewhere.
Doesn't it tell you the line number?
--
Piet van Oostrum <(E-Mail Removed)>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
wanwan
Guest
Posts: n/a
 
      10-14-2005
oops, of course.

Very careless mistake.

thx

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      10-14-2005
wanwan <(E-Mail Removed)> wrote:
...
> when I run my example, an error shows:
> "NameError: global name'menubar' is not defined"
>
> I wonder why it doesn't work. Isn't that the way to define an object
> variable?


The code you posted should not trigger this error. Most likely problem:
you have typed a comma where you meant to type a dot, for example
instead of self.menubar you wrote self,menubar somewhere -- it's a hard
error to spot with certain fonts.


Alex
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting a null pointer, object is most definatly instantiated Justin Java 5 11-02-2006 01:12 AM
How to solve the error "The local variable may not have been instantiated"? Shawn Java 8 09-28-2006 03:28 PM
Stupid Newbie Question: get name of instantiated variable? Ed Dana Java 24 02-03-2005 10:44 PM
ASP.NET application freezes when COM object is instantiated Ralf Müller ASP .Net 3 01-19-2005 09:59 AM
Finding out if web service instantiated object keith chadwick ASP .Net Web Services 0 11-01-2004 05:14 AM



Advertisments