我有在Python一个程序,经由COM从另一个程序(认为Python程序作为一个插件的)余设置该窗口是主要的Python框架的父使得如果其他程序最小化,蟒得到一个窗口句柄框架也会。问题是,当我去退出,并尝试关闭或破坏主机,frame.close从未完成它的执行(虽然它消失)和其他程序拒绝关闭,除非使用任务管理器杀了。

下面大致我们采取以下步骤:

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程序,其他程序关闭细

有帮助吗?

解决方案 3

我对这项决议是有点黑,诚然不是我曾经拿出最完美的解决方案 - 但它的工作原理相当有效...

基本上我的步骤是启动一个线程来轮询看到窗口句柄是否存在与否。虽然它仍然是存在的,什么也不做。如果不再存在,杀蟒应用,允许所述手柄(和主应用程序)被释放。

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

再次感觉有点hackish的,但我真的没有看到它周围的另一种方式。如果任何人有更好的分辨率,请发表。

其他提示

我不知道你Close通话可能被挂在接近处理程序。你有没有打过电话Destroy呢?如果没有帮助,那么唯一的解决办法似乎是“重排根”或“分离”的框架 - 我不明白的方式来做到这一点在wx,但也许你可以下降到WIN32 API为一个任务......?

如果重排根是所有你需要,你可以尝试frame.Reparent(None)frame.Close()

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top