confusion between global names and instantiated object variable names

Discussion in 'Python' started by wanwan, Oct 14, 2005.

  1. wanwan

    wanwan Guest

    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.
    wanwan, Oct 14, 2005
    #1
    1. Advertising

  2. >>>>> "wanwan" <> (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 <>
    URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
    Private email:
    Piet van Oostrum, Oct 14, 2005
    #2
    1. Advertising

  3. wanwan

    wanwan Guest

    oops, of course.

    Very careless mistake.

    thx
    wanwan, Oct 14, 2005
    #3
  4. wanwan <> 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
    Alex Martelli, Oct 14, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ralf Müller
    Replies:
    3
    Views:
    445
    Ralf Müller
    Jan 19, 2005
  2. Ed Dana
    Replies:
    24
    Views:
    1,040
    Bryce
    Feb 3, 2005
  3. Shawn
    Replies:
    8
    Views:
    394
    Shawn
    Sep 28, 2006
  4. Justin
    Replies:
    5
    Views:
    499
    Wesley Hall
    Nov 2, 2006
  5. keith chadwick

    Finding out if web service instantiated object

    keith chadwick, Nov 1, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    137
    keith chadwick
    Nov 1, 2004
Loading...

Share This Page