473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

confusion between global names and instantiated object variable names

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
3 1472
>>>>> "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
oops, of course.

Very careless mistake.

thx

Oct 14 '05 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
2
by: gary.goodwin | last post by:
HI I am trying to understand Attribute usage. For example the class SerializableAttribute is a class correct? So why when it is actually u sed the "Attribute" portion of the name is dropped. The...
3
by: User1014 | last post by:
A global variable is really just a property of the "Global Object", so what does that make a function defined in the global context? A method of the Global Object? ...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
by: ApothecarySW | last post by:
Hello all, I have created a class to track the session details on a database we use at work. The class stores information about the current user, start time, end time, etc. The object...
27
by: Erwin Moller | last post by:
Hi group, Consider this simple script (tested on FF3): <script type="text/javascript"> test = 'outer'; for (var i=0;i<2;i++){ alert(test); var test = 'inner'; alert (test);
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.