windows 我如何获得窗口名称?【巨蟒】

zy1mlcev  于 2023-06-30  发布在  Windows
关注(0)|答案(3)|浏览(761)

如何获取窗口名称?
我想做一个脚本,在那里我有一些键盘事件,我希望它发生,只有当窗口名称中有一些东西,如Firefox。
我该怎么做?
最简单的方法

pobjuy32

pobjuy321#

如果“window name”的意思是“window title",并且假设在hwnd中有窗口的句柄,

import win32gui
thetitle = win32gui.GetWindowText( hwnd )

当然,您需要从here下载win32gui和其他“windows extensions for Python”,并将它们安装到您的机器上。
如果你有别的意思,请澄清!

laawzig2

laawzig22#

使用pyautogui获取window.title

还有一种方法是使用pyautogui模块来获取窗口标题,它支持在Windows上使用
要简单地获取Windows上的所有窗口标题和其他信息,您可以按照下面的片段操作。

安装

$ pip install pyautogui

获取窗口标题

import pyautogui
windows = pyautogui.getAllWindows()
for window in windows:
    print(window.title)

输出:

Windows Powershell
C:\Users\munee\Desktop\ChessMan\main.py (ChessMan) - Sublime Text (UNREGISTERED)
how can I get the window name? [Python] - Stack Overflow - Google Chrome
untitled • (AdvantedScanPro) - Sublime Text (UNREGISTERED)
Recycle Bin
munee
WhatsApp
Realtek Audio Console
Realtek Audio Console

Calculator
Calculator
Microsoft Text Input Application

获取其他窗口属性

为了获得其他窗口属性,您可以继续并参考以下代码片段和输出。

import pyautogui
windows = pyautogui.getAllWindows()
for window in windows:
    print(window)

输出:

<Win32Window left="0", top="0", width="1", height="1", title="">
<Win32Window left="0", top="1030", width="1920", height="50", title="">
<Win32Window left="-9", top="-9", width="1938", height="1048", title="Windows Powershell">
<Win32Window left="-9", top="-9", width="1938", height="1048", title="C:\Users\munee\Desktop\ChessMan\main.py (ChessMan) - Sublime Text (UNREGISTERED)">
<Win32Window left="-9", top="-9", width="1938", height="1048", title="how can I get the window name? [Python] - Stack Overflow - Google Chrome">
<Win32Window left="84", top="78", width="995", height="800", title="untitled • (AdvantedScanPro) - Sublime Text (UNREGISTERED)">
<Win32Window left="467", top="0", width="1424", height="750", title="Recycle Bin">
<Win32Window left="435", top="280", width="1424", height="750", title="munee">
<Win32Window left="320", top="108", width="1280", height="813", title="WhatsApp">
<Win32Window left="0", top="1", width="1280", height="1000", title="Realtek Audio Console">
<Win32Window left="100", top="0", width="1298", height="1010", title="Realtek Audio Console">
<Win32Window left="0", top="0", width="1920", height="1030", title="">
<Win32Window left="0", top="1", width="400", height="665", title="Calculator">
<Win32Window left="1310", top="118", width="418", height="675", title="Calculator">
<Win32Window left="0", top="0", width="1920", height="1080", title="Microsoft Text Input Application">
<Win32Window left="0", top="0", width="0", height="0", title="">
<Win32Window left="19", top="0", width="1901", height="4", title="">
<Win32Window left="0", top="0", width="0", height="0", title="">
<Win32Window left="0", top="0", width="0", height="0", title="">
<Win32Window left="-32000", top="-32000", width="199", height="34", title="Select root@DESKTOP-4UA4732: ~/kabaclassifier">
<Win32Window left="-32000", top="-32000", width="199", height="34", title="mo@DESKTOP-4UA4732: ~">
<Win32Window left="0", top="1", width="1280", height="1000", title="Settings">
<Win32Window left="50", top="0", width="1298", height="1010", title="Settings">
<Win32Window left="0", top="0", width="1920", height="1080", title="">
<Win32Window left="0", top="0", width="1920", height="1080", title="">
<Win32Window left="0", top="0", width="1920", height="1080", title="Program Manager">
njthzxwz

njthzxwz3#

如果使用pyhook,OnKeyboardEvent etc函数中的event.WindowName包含值xx

相关问题