简体   繁体   中英

how can I get the window name? [Python]

How can I get the window name?

I want to make a script where I have some keyboard event, and I want it to happens only if the window name has something in the name, like, Firefox.

how can I do it?

the simplest way

If by "window name" you mean "window title ", and assuming you have the window's handle in hwnd ,

import win32gui
thetitle = win32gui.GetWindowText( hwnd )

You need to download win32gui and the other "windows extensions for Python" from here , of course, and install them on your machine.

If you mean something else, please clarify!

Getting window.title using pyautogui

There is another method using pyautogui module that can be used to get window titles and it supports cross-platform usage on Windows, MacOS and Linux.

To simply get all of the Window titles and other information on the Windows, you can follow the snippet below.

Installation

$ pip install pyautogui

Getting Window Title

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

Output:

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

Getting Other Window Attributes

In order to get other window attributes you can go ahead and refer the following code snippet and output.

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

Output:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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