简体   繁体   English

如何在Python中使用win32gui关闭带句柄的窗口

[英]how do I close window with handle using win32gui in Python

I have obtained the handle of a window I want to target, with win32gui library in Python 我已经使用Python中的win32gui库获得了我想要定位的窗口的句柄

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: 在Python shell中,我得到类似的东西:

2500276L

but if I try 但如果我试试

handle = win32gui.CloseWindow(handle)
print handle

then I get 然后我明白了

2500276

does the 'L' in the end make any difference? “L”到底有什么不同吗?

Thanks for your attention!! 感谢您的关注!!

Try: 尝试:

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

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM