[wxPython-users] Organizing Widget-Bound Methods

Christopher Barker Chris.Barker at noaa.gov
Tue Oct 24 13:16:49 PDT 2006


Rich Shepard wrote:
> These functions are available in three
> ways:
> 
>   1) From the File menu (in the main frame .py file).
>   2) By typing the file name in the text control widget (in the notebook
>     page's .py file).
>   3) By clicking on the "New,", "Open," or "Save" buttons (also in the
>     notebook page's .py file).
> 
>   I'd like to have a single method/function to process each type of action,
> regardless of which route is chosen by the user. That way, a change (or
> error correction) is made in one place and I don't have database creation
> code in multiple modules.

It sounds like the database is a application-global concept, so you're 
right, there should be one place where it gets manipulated. I'd put all 
that code in a single module.

Now each notebook page can import than module, and they'll all be taking 
to the same thing.

You now have two choices:

1) have the event handlers be very simple functions like:

def OnNew(self, event):
	DB_Module.OpenNew()


2) or, as I alluded to before, you don't' actually HAVE to put event 
handlers in the same file as the wx.Panel (or whatever) class. You just 
have to make sure that you reference the function properly:

OpenNewButton.Bind(EVT_BUTTON, DB_Module.OpenNew)

In this case, make sure that DB_Module.OpenNew() can take an event as an 
argument.

I'd be inclined to do (1), while is looks like extra unnecessary code, 
there may well end up being time when it needs to do a bit more, or the 
DB_Module.OpenNew() function needs to now what page was active when it 
was invoked, etc. etc.

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer
                                     		
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov




More information about the wxpython-users mailing list