importing class

Steve Holden steve at holdenweb.com
Mon Oct 30 04:42:14 CET 2006


gmarkowsky at gmail.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 parameterization 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




More information about the Python-list mailing list