confusion between global names and instantiated object variable names

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wanwan

    #1

    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.menub ar, tearoff=0)
    self.menu1.add_ command(label=" Open File", command=donothi ng)
    self.menu1.add_ separator()
    self.menu1.add_ command(label=" Exit", command=root.qu it)
    self.menubar.ad d_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.

  • Piet van Oostrum

    #2
    Re: confusion between global names and instantiated object variable names

    >>>>> "wanwan" <ericwan78@yaho o.com> (w) wrote:
    [color=blue]
    >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:[/color]

    [color=blue]
    >w> class mygui:[/color]

    [color=blue]
    >w> def __init__(self, root):[/color]
    [color=blue]
    >w> self.menubar = Menu(root)[/color]
    [color=blue]
    >w> # Game Menu
    >w> self.menu1 = Menu(self.menub ar, tearoff=0)
    >w> self.menu1.add_ command(label=" Open File", command=donothi ng)
    >w> self.menu1.add_ separator()
    >w> self.menu1.add_ command(label=" Exit", command=root.qu it)
    >w> self.menubar.ad d_cascade(label ="File", menu=self.menu1 )[/color]
    [color=blue]
    >w> # ignoring the rest of the program ...[/color]

    [color=blue]
    >w> when I run my example, an error shows:
    >w> "NameError: global name'menubar' is not defined"[/color]

    If it talks about global name, it can't be self.menubar or
    anything.menuba r. So there must be a soloist menubar reference somewhere.
    Doesn't it tell you the line number?
    --
    Piet van Oostrum <piet@cs.uu.n l>
    URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
    Private email: piet@vanoostrum .org

    Comment

    • wanwan

      #3
      Re: confusion between global names and instantiated object variable names

      oops, of course.

      Very careless mistake.

      thx

      Comment

      • Alex Martelli

        #4
        Re: confusion between global names and instantiated object variable names

        wanwan <ericwan78@yaho o.com> wrote:
        ...[color=blue]
        > 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?[/color]

        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

        Comment

        Working...