By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

confusion between global names and instantiated object variable names

P: n/a
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.

Oct 14 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
>>>>> "wanwan" <er*******@yahoo.com> (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 <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Oct 14 '05 #2

P: n/a
oops, of course.

Very careless mistake.

thx

Oct 14 '05 #3

P: n/a
wanwan <er*******@yahoo.com> 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
Oct 14 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.