1

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

以下是我们大致采取的步骤:

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

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

我很感激这方面的任何帮助!

[编辑]只有当我使用句柄作为父级时才会出现这种情况,如果我只是获取句柄并关闭python程序,其他程序关闭正常

4

3 回答 3

1

我想知道您的Close电话是否可能挂在近距离处理程序中。你试过打电话Destroy吗?如果这没有帮助,那么唯一的解决方案似乎是“重新设置”或“分离”你的框架 - 我没有看到这样做的方法wx,但也许你可以为那个下拉到 win32 API任务...?

于 2009-06-02T20:15:58.273 回答
0

我对此的解决方案有点被破解了,并且诚然这不是我想出的最优雅的解决方案 - 但它非常有效......

基本上我的步骤是启动一个线程来轮询以查看窗口句柄是否存在。虽然它仍然存在,但什么也不做。如果它不再存在,则终止 python 应用程序,允许释放句柄(和主应用程序)。

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

再一次,感觉有点骇人听闻,但我真的没有看到另一种解决方法。如果其他人有更好的解决方案,请发布。

于 2009-11-10T21:48:13.053 回答
0

如果您只需要重新养育子女,您可以frame.Reparent(None)先尝试frame.Close()

于 2009-06-04T20:46:21.140 回答