Quick Tip: Controlling Windows with Python — SitePoint (2024)

In this quick tip, excerpted from Useful Python, Stuart looks at ways to control the Windows OS with Python.

The Windows Registry

Windows is entirely controllable from code using the Win32 API, and Microsoft provides extensive documentation at Microsoft Docs for everything that Windows can programmatically do. All of this is accessible from Python as well, although it can seem a little impenetrable if we’re not already accustomed to the Win32 API’s particular way of working. Fortunately, there are various wrappers for these low-level APIs to make code easier to write for Python programmers.

A simple example is to interact with the Windows Registry. Python actually includes the winreg module for doing this out of the box, so no extra installation is required. For an example, let’s check where the Program Files folder actually lives:

>>> import winreg>>> hive = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)>>> key = winreg.OpenKey(hive, r"SOFTWARE\Microsoft\Windows\CurrentVersion")>>> value, type = winreg.QueryValueEx(key, "ProgramFilesDir")>>> value'C:\\Program Files'

Raw Strings

In the code above, we’re using “raw strings” to specify the key name:

r"SOFTWARE\Microsoft\Windows\CurrentVersion"

Strings passed to the Win32 API often include the backslash character (\), because Windows uses it in file paths and registry paths to divide one directory from the next.

However, Python uses a backslash as an escape character to allow adding special, untypeable characters to a string. For example, the Python string "first line\nsecond line" is a string with a newline character in it, so that the text is spread over two lines. This would conflict with the Windows path character: a file path such as "C:\newdir\myfile.txt" would have the \n interpreted as a newline.

Raw strings avert this: prefixing a Python string with r removes the special meaning of a backslash, so that r"C:\newdir\myfile.txt" is interpreted as intended. We can see that backslashes are treated specially by the value we get back for the folder location: it’s printed as 'C:\\Program Files'—with the backslash doubled to remove its special meaning—but this is how Python prints it rather than the actual value. Python could have printed that as r'C:\Program Files' instead.

The Windows API

Reading the registry (and even more so, writing to it) is the source of a thousand hacks on web pages (many of which are old, shouldn’t be linked to, and use the ancient REGEDT32.EXE), but it’s better to actually use the API for this. (Raymond Chen has written many long sad stories about why we should use the API and not the registry.) How would we use the Win32 API from Python to work this out?

The Win32 Python API is available in the PyWin32 module, which can be obtained with python -m pip install pywin32. The documentation for the module is rather sparse, but the core idea is that most of the Windows Shell API (that’s concerned with how the Windows OS is set up) is available in the win32com.shell package. To find out the location of the Program Files folder, MSDN shows that we need the SHGetKnownFolderPath function, to which is passed a KNOWNFOLDERID constant and a flag set to 0. Shell constants are available to Python in win32com.shell.shellcon (for “shell constants”), which means that finding the Program Files folder requires just one (admittedly complex) line:

>>> from win32com.shell import shell, shellcon>>> shell.SHGetKnownFolderPath(shellcon.FOLDERID_ProgramFiles, 0)"C:\\Program Files"

Digging around in the depths of the Win32 API gives us access to anything we may want to access in Windows (including windows!), but as we’ve seen, it can be quite complicated to find out how to do what we need to, and then to translate that need into Python. Fortunately, there are wrapper libraries for many of the functions commonly used. One good example is PyGetWindow, which allows us to enumerate and control on-screen windows. (It claims to be cross-platform, but it actually only works on Windows. But that’s all we need here.)

We can install PyGetWindow with python -m pip install pygetwindow, and then list all the windows on screen and manipulate them:

>>> import pygetwindow as gw>>> allMSEdgeWindows = gw.getWindowsWithTitle("edge")>>> allMSEdgeWindows[Win32Window(hWnd=197414), Win32Window(hWnd=524986)]>>> allMSEdgeWindows[0].title'pywin32 · PyPI - Microsoft Edge'>>> allMSEdgeWindows[1].title'Welcome to Python.org - Microsoft Edge'

Those windows can be controlled. A window object can be minimized and restored, or resized and moved around the screen, and focused and brought to the front:

>>> pythonEdgeWindow = allMSEdgeWindows[1]>>> pythonEdgeWindow.minimize()>>> pythonEdgeWindow.restore()>>> pythonEdgeWindow.sizeSize(width=1050, height=708)>>> pythonEdgeWindow.topleftPoint(x=218, y=5)>>> pythonEdgeWindow.resizeTo(800, 600)

It’s always worth looking on PyPI for wrapper modules that provide a more convenient API for whatever we’re trying to do with windows or with Windows. But if need be, we have access to the whole Win32 API from Python, and that will let us do anything we can think of.

This article is excerpted from Useful Python, available on SitePoint Premium and from ebook retailers.

Frequently Asked Questions (FAQs) about Controlling Windows with Python

How Can I Install the PyGetWindow Module?

To install the PyGetWindow module, you need to use pip, which is a package installer for Python. Open your command prompt or terminal and type the following command: pip install pygetwindow. If you have both Python 2 and Python 3 installed, use pip3 install pygetwindow to ensure you’re installing for the correct version.

What Functions are Available in the PyGetWindow Module?

The PyGetWindow module provides several functions for controlling and interacting with windows. Some of the key functions include getWindowsWithTitle(), getWindowsAt(), getAllWindows(), getActiveWindow(), and getFocusedWindow(). Each of these functions serves a unique purpose in identifying and manipulating windows.

How Can I Get a Specific Window Using PyGetWindow?

To get a specific window, you can use the getWindowsWithTitle() function. This function returns a list of Window objects that have a title matching the string you passed. For example, pygetwindow.getWindowsWithTitle('My Application') would return all windows with the title ‘My Application’.

How Can I Minimize or Maximize a Window Using PyGetWindow?

PyGetWindow provides the minimize() and maximize() methods for Window objects. To use these methods, you first need to get the Window object for the window you want to manipulate. Once you have the Window object, you can call myWindow.minimize() or myWindow.maximize() to minimize or maximize the window, respectively.

How Can I Move a Window to a Specific Location on the Screen?

To move a window, you can use the moveTo() method of a Window object. This method takes two arguments: the x and y coordinates of the new location. For example, myWindow.moveTo(100, 200) would move the window to the point (100, 200) on the screen.

How Can I Resize a Window Using PyGetWindow?

To resize a window, you can use the resizeTo() method of a Window object. This method takes two arguments: the new width and height of the window. For example, myWindow.resizeTo(800, 600) would resize the window to be 800 pixels wide and 600 pixels tall.

How Can I Bring a Window to the Front?

To bring a window to the front, you can use the bringToFront() method of a Window object. This method doesn’t take any arguments. For example, myWindow.bringToFront() would bring the window to the front of all other windows.

How Can I Check if a Window is Visible or Not?

To check if a window is visible, you can use the isVisible attribute of a Window object. This attribute returns a boolean value indicating whether the window is visible or not. For example, myWindow.isVisible would return True if the window is visible, and False otherwise.

How Can I Close a Window Using PyGetWindow?

PyGetWindow doesn’t provide a direct method to close windows. However, you can use the pyautogui module in conjunction with PyGetWindow to close a window. First, bring the window to the front using myWindow.bringToFront(), then use pyautogui.hotkey('alt', 'f4') to send the Alt+F4 command to the window, which will close it.

Can I Use PyGetWindow with Other GUI Automation Libraries?

Yes, PyGetWindow can be used in conjunction with other GUI automation libraries like pyautogui and pynput. These libraries provide additional functionality for controlling the mouse and keyboard, which can be useful for automating tasks that involve interacting with windows.

Quick Tip: Controlling Windows with Python — SitePoint (2024)
Top Articles
Windows 11 requirements
YeFi CEO: Ethereum to be 'global computer' bigger than AWS, Azure, and Google combined
Tryst Utah
Fort Carson Cif Phone Number
Collision Masters Fairbanks
2024 Fantasy Baseball: Week 10 trade values chart and rest-of-season rankings for H2H and Rotisserie leagues
Jennette Mccurdy And Joe Tmz Photos
Beautiful Scrap Wood Paper Towel Holder
Zitobox 5000 Free Coins 2023
South Carolina defeats Caitlin Clark and Iowa to win national championship and complete perfect season
Gameplay Clarkston
Hardly Antonyms
Fire Rescue 1 Login
W303 Tarkov
The Witcher 3 Wild Hunt: Map of important locations M19
Amc Flight Schedule
Pizza Hut In Dinuba
Jinx Chapter 24: Release Date, Spoilers & Where To Read - OtakuKart
Carson Municipal Code
Ibukunore
Royal Cuts Kentlands
Hermitcraft Texture Pack
Glenda Mitchell Law Firm: Law Firm Profile
Dwc Qme Database
Universal Stone Llc - Slab Warehouse & Fabrication
Sussyclassroom
Mta Bus Forums
800-695-2780
Wku Lpn To Rn
134 Paige St. Owego Ny
Warren County Skyward
Craigslist Gigs Norfolk
Solarmovie Ma
Tendermeetup Login
Selfservice Bright Lending
Manatee County Recorder Of Deeds
Ksu Sturgis Library
„Wir sind gut positioniert“
Gun Mayhem Watchdocumentaries
Craigslist Pa Altoona
18 terrible things that happened on Friday the 13th
Ferguson Employee Pipeline
Rhode Island High School Sports News & Headlines| Providence Journal
Lcwc 911 Live Incident List Live Status
Frigidaire Fdsh450Laf Installation Manual
Rocket Lab hiring Integration & Test Engineer I/II in Long Beach, CA | LinkedIn
What Time Do Papa John's Pizza Close
Rétrospective 2023 : une année culturelle de renaissances et de mutations
Morbid Ash And Annie Drew
Bluebird Valuation Appraiser Login
Rise Meadville Reviews
Ocean County Mugshots
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 5570

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.