pythonwx关闭窗口,wxPython不会与作为窗口句柄的父项关闭Frame

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?

I appreciate any help with this!

[Edit]

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...

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值