输出内容只包含句柄,且以逗号隔开,在一行内输出
时间: 2024-02-17 17:00:30 浏览: 26
以下是修改后的代码,输出内容只包含句柄,且以逗号隔开,在一行内输出:
```python
import win32gui
class Hwnd():
def get_hwnd_dic(self, hwnd, hwnd_list):
if (win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd)):
title = win32gui.GetWindowText(hwnd)
if self.target_title in title:
hwnd_list.append(str(hwnd))
def get_hwnd(self, target_title):
'''
:param target_title: 目标窗口标题,类型为str
:return: "hwnd1, hwnd2, ..."
'''
self.target_title = target_title
hwnd_list = []
win32gui.EnumWindows(self.get_hwnd_dic, hwnd_list)
return ", ".join(hwnd_list)
hwnd = Hwnd()
target_title = "指定窗口标题"
hwnd_str = hwnd.get_hwnd(target_title)
print(f"指定标题为'{target_title}'的窗口句柄列表: {hwnd_str}")
```
这里修改了get_hwnd_dic()方法,在找到指定标题的窗口时,将该窗口的句柄转换为字符串并添加到hwnd_list列表中。修改后的get_hwnd()方法将hwnd_list转换为以逗号隔开的字符串,并返回该字符串。在主程序中实例化Hwnd类,传入指定的标题,调用get_hwnd()方法获取指定标题的窗口句柄列表,并将其打印出来。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)