Some wxWindows Theory

Hi All,

Let me start by saying I have done quite a bit of wxPython development, but all of my knowledge is self-taught, and I do not have an in-depth background of the Win32 API (I am in the middle of that book right now...OK well not quite the middle..)

Anyhow, I noticed a thread awhile back where Robin mentioned the function wxWindow_FromHWND() that really piqued my interest for a project I am working on in my spare time. First of all, I realize that this is totally unsupported stuff, but I was hoping to get a little feedback into the "physics" (if you will) behind my particular problem. I have been messing around with Python and pythoncom, having a largish commercial application talking to my simple Python COM server. What I ultimately want to do is create my own little "toolkit" embedded inside this application which will allow me to run Python scripts directly, automating tasks through this particular app's documented COM API.

Since I know wxPython fairly well, I dreamed of using it but thought it impossible. THEN, I got wind of the wxWindow_FromHWND() function. To make a fairly long story short, I quickly found I could grab the window handle of my app with the win32ui extensions, and create a wxWindow wrapper around my main application window. I could even create a simple popup window or wxMiniFrame inside the other application. My heart started beating slightly faster and my mouth started to water.... Then, inevitable disaster struck - when events are involved, everything goes haywire. It appears that as soon as any events on the wxPython side are fired things go to crap. When I think about it, it all makes sense. I have no wxApp, and therefore no mainloop running to process events. I started experimenting with firing up a wxApp in a new thread and incorporating the wrapped wxWindow, but it was only getting worse, and it was late.

My question is, should this be possible (or am I wasting my time), and what would be the general concept to make this work? Do I need to involve a wxApp somehow? Can I create my own event loop somehow, without a wxApp? Is it only the events that are thrown up to a non-existent higher-level handler that are my problem (I have a feeling this may be the case)? Any fodder you guys can provide for my weekend hacking would be appreciated. Essentially, here is what I am doing in my code:

My app starts and connects to my Python COM server, then does the following:

try:
    myCWnd = win32ui.GetForegroundWindow()
    self.wxWindowWrapper = wxWindow_FromHWND(myCWnd.GetSafeHwnd())
    # The custom panel
    self.wxwin = MyMiniFrame(self.wxWindowWrapper, "This is a wxMiniFrame",
                      style=wxDEFAULT_FRAME_STYLE | wxTINY_CAPTION_HORIZ)
    self.wxwin.Show(True)
except:
    win32ui.MessageBox("Oops: %s,%s" % (sys.exc_info()[0], sys.exc_info()[1]))

This works, and shows a simple wxMiniFrame which I can move around, but the little 'x' (the Close button) does nothing to the wxMiniFrame. Things chug along fine, but if I minimize the app (the miniframe goes with it), then restore the app....CRASH. This is obviously related to the native paint events (or something) being sent to the wxMiniFrame.

Anyway, thanks in advance for any musings.
Mark.

Mark Melvin wrote:

My question is, should this be possible (or am I wasting my time), and what would be the general concept to make this work?

I think it is meant to be used in a different way, wrapping foreign windows into a wxWindows application. Mixing the wx framework into a non wx application will have a number of problems, I'm sure. Of course the best place for answers to questions that come up about this will be the C++ source...

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

I
started experimenting with firing up a wxApp in a new thread and
incorporating the wrapped wxWindow, but it was only getting worse,
and
it was late.

This is a shot in the dark, but in Programming Python there was some
discussion of the os module and various 'exec*()' and 'spawn*()'
functions. Have you tried to use these instead of the 'thread' calls?

···

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.

Stephen Scherer wrote:

I started experimenting with firing up a wxApp in a new thread and incorporating the wrapped wxWindow, but it was only getting worse,
and it was late.
   
This is a shot in the dark, but in Programming Python there was some
discussion of the os module and various 'exec*()' and 'spawn*()'
functions. Have you tried to use these instead of the 'thread' calls?

That's a good point. I have done some more thinking about this and I think I can still do this the other way around (i.e. a wxApp, using the wrapped window). The catch is, I wonder if I can attach wxEvents to the wrapped window. If I can, I should be able to do everything I want to do. I want my wxApp to appear to be a child of the wrapped application/window, so that it minimizes and restores with the wrapped application. Essentially it will appear as a little floating toolbar on top of the application I am wrapping. I'll have to play with it some more.

Mark.