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.