OOP Newbie mixing COM, wxPython, BOA

Jeff Shannon jeff at ccvcorp.com
Tue Feb 12 02:29:43 CET 2002

Mojo wrote:

> The subject line could easily have been "fool in way over his head..."
> That said, I got farther than I thought possible only to be stymied by what
> I'd guess is either an idiom I don't understand (being an OOP newbie) or a
> question of variable scope/namespace...
> My problem is that my app has two classes (a class encapsulating the GUI,
> and a class that implements an event handler for a COM object.)  I don't
> know how to access GUI objects in the former, from code in the latter.


> How do I call the "AddRoot" method of the "buddyTree" object in the VpFrame
> class, from the separate class "JabberEvents"?  Any help, pointers,
> explanations greatly appreciated.

This is pretty much a namespace issue, and has nothing to do with COM, and only
indirectly with wxPython/Boa...

What you should do, is instead of making the Jabber object be global to your
VpFrame module, you should encapsulate its creation into a function.  Have your
frame call this function in its __init__(), and then save the reference on the
frame.  At this point, you will also want to stick a reference to the frame onto
the Jabber object.  Something like this:

def InitializeJabber():
    jab = win32com.client.DispatchWithEvents("JabberCOM.JabberSession",
    return jab

##  in class VpFrame...
        def __init__(self, parent):
            self.jabber = InitializeJabber()
            self.jabroster = self.jabber.Roster
            self.jabber.frame = self

Note that you'll need to change all your references to 'jab' and 'jabroster' to
use this saved reference.

Now, in any of your Jabber object's methods, you can do


A few design tips to keep in mind--you want to have the minimum possible number
of connections between your frame and the jabber object.  Don't have the jabber
object do each step needed when you connect, for instance;  write a VpFrame
method called OnConnect() that handles everything the GUI needs to do, and have
jabber call that single method.  Similarly for any other events that you receive
from Jabber--the Jabber object handles the protocol side of things, and then
tells the frame object to handle the GUI side of things.  In fact, you might
look at defining custom wxPython command events, and having the Jabber object
notify the GUI by posting those events. (This is a somewhat advanced subject,
which essentially does the same thing--calls a specified method--but allows the
GUI side a lot more flexibility in determining where and how to handle the
event.  This could really pay off, though, if you're intending on expanding the
application considerably.)

Also, by having both objects holding references to each other, we've created a
reference cycle.  Python's garbage collector should be able to handle this okay,
but just in case, it's probably a good idea to explicitly destroy the Jabber
object in your frame's OnCloseWindow() handler (or whatever name is given to the
method that shuts down your frame).  But it's a common idiom in wxPython for
parent and child windows to each have references to the other.  (Typically, the
wxWindows framework will clean up child windows for you, but obviously wxWindows
knows nothing of your Jabber object...)

Jeff Shannon
Credit International

More information about the Python-list mailing list