importing class

gmarkowsky at gmail.com gmarkowsky at gmail.com
Sun Oct 29 11:40:46 EST 2006


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.828597.57280 at b28g2000cwb.googlegroups.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.pack(side=LEFT)
> >
> >         self.hi_there = Button(frame, text=text_2, command=self.comm_2)
> >         self.hi_there.pack(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




More information about the Python-list mailing list