wxPython不会与作为窗口句柄的父项关闭Frame [英] wxPython won't close Frame with a parent who is a window handle

查看:388
本文介绍了wxPython不会与作为窗口句柄的父项关闭Frame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python程序,该程序通过COM从另一个程序获取窗口句柄(认为Python程序是一个插件),我将此窗口设置为Python主框架的父窗口,这样,如果另一个程序最小化,则python框架也会.问题是当我退出并尝试关闭或破坏主框架时,frame.close永远不会完成其执行(尽管它确实消失了),而另一个程序则拒绝关闭,除非被TaskManager杀死.

I have a program in Python that gets a window handle via COM from another program (think of the Python program as an addin) I set this window to be the main Python frame's parent so that if the other program minimizes, the python frame will too. The problem is when I go to exit, and try to close or destroy the main frame, the frame.close never completes it's execution (although it does disappear) and the other program refuses to close unless killed with TaskManager.

这里大致是我们采取的步骤:

Here are roughly the steps we take:

if we are started directly, launch other program
if not, we are called from the other program, do nothing

enter main function:
create new wx.App
set other program as frame parent:
  Get handle via COM
  create a parent using wx.Window_FromHWND
  create new frame with handle as parent
  show frame
enter main loop

App.onexit:
  close frame
  frame = None
  handle as parent = None
  handle = None

有人对此有任何想法或经验吗?

Anybody have any thoughts on this or experience with this sort of thing?

我对此表示感谢!

只有当我将句柄用作父对象时,如果我只是获得了句柄并关闭了python程序,其他程序就很好了

This is only the case when I use the handle as a parent, if I just get the handle and close the python program, the other program closes fine

推荐答案

我对此的解决方法有些微不足道,并且公认不是我想出的最优雅的解决方案-但却有效.

My resolution to this is a little bit hacked, and admittedly not the most elegant solution that I've ever come up with - but it works rather effectively...

基本上,我的步骤是启动一个轮询以查看窗口句柄是否存在的线程.虽然它仍然存在,但是什么也不做.如果不再存在,请杀死python应用程序,并释放该句柄(和主应用程序).

Basically my steps are to start a thread that polls to see whether the window handle is existent or not. While it's still existent, do nothing. If it no longer exists, kill the python application, allowing the handle (and main application) to be released.

class CheckingThread(threading.Thread):
    '''
    This class runs a check on Parent Window to see if it still is running
    If Parent Window closes, this class kills the Python Window application in memory
    '''
    def run(self):
        '''
        Checks Parent Window in 5 seconds intervals to make sure it is still alive.
        If not alive, exit application
        '''
        self.needKill = False

        while not self.needKill:
            if self.handle is not None:
                if not win32gui.IsWindow(self.handle):
                    os._exit(0)
                    break
            time.sleep(5)

    def Kill(self):
        '''
        Call from Python Window main application that causes application to exit
        '''
        self.needKill = True

    def SetHandle(self, handle):
        '''
        Sets Handle so thread can check if handle exists.
        This must be called before thread is started.
        '''
        self.handle = handle

再次,这感觉有点骇人听闻,但我真的看不到其他解决方法.如果其他人有更好的分辨率,请发布.

Again, it feels a little hackish, but I don't really see another way around it. If anybody else has better resolutions, please post.

这篇关于wxPython不会与作为窗口句柄的父项关闭Frame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆