importing class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gmarkowsky@gmail.com

    importing class

    Hi all,

    I'm trying to import a class from a module. The class looks like this:
    class App:

    def __init__(self, master):

    frame = Frame(master)
    frame.pack()

    self.button = Button(frame, text=text_1, command= self.comm_1)
    self.button.pac k(side=LEFT)

    self.hi_there = Button(frame, text=text_2, command=self.co mm_2)
    self.hi_there.p ack(side=LEFT)

    def comm_1(self):
    command1()
    root.quit()

    def comm_2(self):
    command2()
    root.quit()

    It's supposed to just make a Tkinter window with two choices. The
    problem is that when I import it from a module, I get the following
    error:

    NameError: global name 'Frame' is not defined

    But when I copy and paste it into the file, it works. Can anyone tell
    me what's wrong?

    Greg

  • Marc 'BlackJack' Rintsch

    #2
    Re: importing class

    In <1161966120.828 597.57280@b28g2 000cwb.googlegr oups.com>, gmarkowsky
    wrote:
    Hi all,
    >
    I'm trying to import a class from a module. The class looks like this:
    class App:
    >
    def __init__(self, master):
    >
    frame = Frame(master)
    frame.pack()
    >
    self.button = Button(frame, text=text_1, command= self.comm_1)
    self.button.pac k(side=LEFT)
    >
    self.hi_there = Button(frame, text=text_2, command=self.co mm_2)
    self.hi_there.p ack(side=LEFT)
    >
    def comm_1(self):
    command1()
    root.quit()
    >
    def comm_2(self):
    command2()
    root.quit()
    >
    It's supposed to just make a Tkinter window with two choices. The
    problem is that when I import it from a module, I get the following
    error:
    >
    NameError: global name 'Frame' is not defined
    >
    But when I copy and paste it into the file, it works. Can anyone tell
    me what's wrong?
    Yes, the global name `Frame` is not defined. `Frame` is a name in the
    `Tkinter` module and you have to import it to reference it. Add the
    following import statement to your file:

    from Tkinter import Frame, Button

    You use `Button` too and this also lives in the `Tkinter` module.

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • gmarkowsky@gmail.com

      #3
      Re: importing class

      Yep, that fixed it. Many thanks.

      Greg

      Dennis Lee Bieber wrote:
      On 27 Oct 2006 09:22:00 -0700, gmarkowsky@gmai l.com declaimed the
      following in comp.lang.pytho n:
      >
      It's supposed to just make a Tkinter window with two choices. The
      problem is that when I import it from a module, I get the following
      error:

      NameError: global name 'Frame' is not defined

      But when I copy and paste it into the file, it works. Can anyone tell
      me what's wrong?
      Probably the simple fact that your "file" likely has all the imports
      for Tkinter defined. The module that you are importing needs to have
      those imports inside it -- imported modules do not have visibility of
      names defined in the importING file.
      --
      Wulfraed Dennis Lee Bieber KD6MOG
      wlfraed@ix.netc om.com wulfraed@bestia ria.com

      (Bestiaria Support Staff: web-asst@bestiaria. com)
      HTTP://www.bestiaria.com/

      Comment

      • gmarkowsky@gmail.com

        #4
        Re: importing class

        Thanks, I got that part. The problem I'm still having is that it's not
        seeing things like text_1, which are defined in the program. How can I
        make it see that?

        Another question I should ask is whether I should even bother doing
        this. That is, it seems that the elegant and approved way of doing this
        kind of thing may be to put a class in a module and then just use the
        module over and over again in programs. I'm making a few GUIs which
        present two options and ask the user to chose one, so I thought I could
        just do it this way. Of course I could very easily just copy and paste
        the class into each file, but that seems silly. I haven't had any
        trouble using modules for functions, but for classes it is not working
        right so far, and I'm having trouble finding examples to follow.

        Greg

        Marc 'BlackJack' Rintsch wrote:
        In <1161966120.828 597.57280@b28g2 000cwb.googlegr oups.com>, gmarkowsky
        wrote:
        >
        Hi all,

        I'm trying to import a class from a module. The class looks like this:
        class App:

        def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text=text_1, command= self.comm_1)
        self.button.pac k(side=LEFT)

        self.hi_there = Button(frame, text=text_2, command=self.co mm_2)
        self.hi_there.p ack(side=LEFT)

        def comm_1(self):
        command1()
        root.quit()

        def comm_2(self):
        command2()
        root.quit()

        It's supposed to just make a Tkinter window with two choices. The
        problem is that when I import it from a module, I get the following
        error:

        NameError: global name 'Frame' is not defined

        But when I copy and paste it into the file, it works. Can anyone tell
        me what's wrong?
        >
        Yes, the global name `Frame` is not defined. `Frame` is a name in the
        `Tkinter` module and you have to import it to reference it. Add the
        following import statement to your file:
        >
        from Tkinter import Frame, Button
        >
        You use `Button` too and this also lives in the `Tkinter` module.
        >
        Ciao,
        Marc 'BlackJack' Rintsch

        Comment

        • Steve Holden

          #5
          Re: importing class

          gmarkowsky@gmai l.com wrote:
          Thanks, I got that part. The problem I'm still having is that it's not
          seeing things like text_1, which are defined in the program. How can I
          make it see that?
          >
          Your module is intended to work with many different main programs, so it
          shouldn't make any assumptions about the names that the main program
          uses for things. That would be rather bad programming style ("rigind
          coupling" is something to be avoided where possible). I wouldn't call
          that class App just because it's misleading: maybe you could change the
          name to YesNo, or Choice, or something more indicative of its function?

          You could pass text_1 and text_2 as arguments to the class's __init__
          method - that way you could just use them directly.
          Another question I should ask is whether I should even bother doing
          this. That is, it seems that the elegant and approved way of doing this
          kind of thing may be to put a class in a module and then just use the
          module over and over again in programs. I'm making a few GUIs which
          present two options and ask the user to chose one, so I thought I could
          just do it this way. Of course I could very easily just copy and paste
          the class into each file, but that seems silly. I haven't had any
          trouble using modules for functions, but for classes it is not working
          right so far, and I'm having trouble finding examples to follow.
          >
          Seems like parameterizatio n is the thing you are missing. Change the
          __init__ method declaration to

          def __init__(self, master, text_1="OK", text_2="Cancel" ):
          ...

          leaving the rest of the code the same. (Though I note your module also
          fails to define a "command1" and "command2" function, this may just be
          because you are only quoting partial code).

          Then in your main program create the object with

          myDialog = YesNo(master, "Yes", "No")

          Looks like you are new to Python - perseverre and you will pick it up
          quite quickly!

          regards
          Steve
          --
          Steve Holden +44 150 684 7255 +1 800 494 3119
          Holden Web LLC/Ltd http://www.holdenweb.com
          Skype: holdenweb http://holdenweb.blogspot.com
          Recent Ramblings http://del.icio.us/steve.holden

          Comment

          • gmarkowsky@gmail.com

            #6
            Re: importing class

            Thanks for your help. Actually my idea was that command1 and command2
            would be defined within the program, not the module, as I would have
            different choices in different programs. Should I pass them in as a
            parameter too?

            Greg

            Steve Holden wrote:
            gmarkowsky@gmai l.com wrote:
            Thanks, I got that part. The problem I'm still having is that it's not
            seeing things like text_1, which are defined in the program. How can I
            make it see that?
            Your module is intended to work with many different main programs, so it
            shouldn't make any assumptions about the names that the main program
            uses for things. That would be rather bad programming style ("rigind
            coupling" is something to be avoided where possible). I wouldn't call
            that class App just because it's misleading: maybe you could change the
            name to YesNo, or Choice, or something more indicative of its function?
            >
            You could pass text_1 and text_2 as arguments to the class's __init__
            method - that way you could just use them directly.
            >
            Another question I should ask is whether I should even bother doing
            this. That is, it seems that the elegant and approved way of doing this
            kind of thing may be to put a class in a module and then just use the
            module over and over again in programs. I'm making a few GUIs which
            present two options and ask the user to chose one, so I thought I could
            just do it this way. Of course I could very easily just copy and paste
            the class into each file, but that seems silly. I haven't had any
            trouble using modules for functions, but for classes it is not working
            right so far, and I'm having trouble finding examples to follow.
            Seems like parameterizatio n is the thing you are missing. Change the
            __init__ method declaration to
            >
            def __init__(self, master, text_1="OK", text_2="Cancel" ):
            ...
            >
            leaving the rest of the code the same. (Though I note your module also
            fails to define a "command1" and "command2" function, this may just be
            because you are only quoting partial code).
            >
            Then in your main program create the object with
            >
            myDialog = YesNo(master, "Yes", "No")
            >
            Looks like you are new to Python - perseverre and you will pick it up
            quite quickly!
            >
            regards
            Steve
            --
            Steve Holden +44 150 684 7255 +1 800 494 3119
            Holden Web LLC/Ltd http://www.holdenweb.com
            Skype: holdenweb http://holdenweb.blogspot.com
            Recent Ramblings http://del.icio.us/steve.holden

            Comment

            • Steve Holden

              #7
              Re: importing class

              gmarkowsky@gmai l.com wrote:
              Thanks for your help. Actually my idea was that command1 and command2
              would be defined within the program, not the module, as I would have
              different choices in different programs. Should I pass them in as a
              parameter too?
              >
              It would seem to be the only way to tell your object which callbacks to
              call!

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC/Ltd http://www.holdenweb.com
              Skype: holdenweb http://holdenweb.blogspot.com
              Recent Ramblings http://del.icio.us/steve.holden

              Comment

              Working...