how do I close window with handle using win32gui in Python

谁都会走 提交于 2019-12-07 09:16:41

问题


I have obtained the handle of a window I want to target, with win32gui library in Python

How do I close the window?

I have the following code, the second line did what I intended to do

but the last line seems to be wrong.

handle = win32gui.FindWindow(None, r'Notepad++')
win32gui.SetForegroundWindow(handle)
win32gui.CloseWindow(handle)

I also want to know if I just want to close the window, is the second line necessary?

Besides that, I notice a minor thing, and I am curious about it:

If I try

win32gui.CloseWindow(handle)

in Python shell, I get something like:

2500276L

but if I try

handle = win32gui.CloseWindow(handle)
print handle

then I get

2500276

does the 'L' in the end make any difference?

Thanks for your attention!!


回答1:


Try:

import win32con    
win32gui.PostMessage(handle,win32con.WM_CLOSE,0,0)

This should work.



来源:https://stackoverflow.com/questions/27586411/how-do-i-close-window-with-handle-using-win32gui-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!