Suppose you have to change your display resolution reasonably often. In that case, it can be a bit tiring to open the Windows display settings, choose the preferred resolution and confirm that you want to keep this resolution. But you can circumvent the Windows display settings with a small Python script.
TL;DR
You can find the entire code together with a short setup script in my GitHub repository
Change Windows Display Resolution with Python
The Python script solution uses the pywin32
package, which provides access to the Windows API from Python. The package is easily installed into a virtual environment with pip:
1
2
3
python -m venv .venv
.\.venv\Scripts\activate
python -m pip install pywin32
Changing the Windows display resolution involves getting a DEVMODE
object which is a data structure used in the Windows API to pass settings to a device, here the display, settings the width and height of the display resolution, and then passing the DEVMODE
object to ChangeDisplaySettings(...)
. Luckily, Peter Wood over on Stack Overflow already provided a Python code snippet which changes the display resolution using the pywin32
package:
1
2
3
4
5
6
7
8
9
10
11
import pywintypes
import win32api
import win32con
devmode = pywintypes.DEVMODEType()
devmode.PelsWidth = 2560
devmode.PelsHeight = 1440
devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT
win32api.ChangeDisplaySettings(devmode, 0)
In lines 6 and 7 the new display resolution is set using devmode.PelsWidth
and devmode.PelsHeight
for the width and height in pixels.
Make sure to pick a resolution your monitor supports before running the script; otherwise, nothing happens.
Adding a Python Application to the Windows 10/11 System Tray
Manually running the code above is only slightly better or even worse than using the Windows display settings to change the resolution. Therefore we want to add this Python application to the Windows system tray to be easily accessed via a right-click. Adding a Python application to the system tray is pretty straightforward using the package pystray
that provides an operating agnostic API to add a Python application to the system tray. Installing it can also be done using pip:
1
python -m pip install pystray
The following code was adapted from Sebastin Ignacio Camilla Trinc (Stack Overflow):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import pystray
from PIL import Image
from pystray import MenuItem as item
def on_quit():
icon.visible = False
icon.stop()
image = Image.open("icon.png")
menu = (
item('MenuItem0', None),
item('MenuItem1', None),
item('Quit', on_quit)
)
icon = pystray.Icon("name", image, "title", menu)
icon.run()
First, the Python code needs an image file (256x256 px) as an icon for the system tray in line 10. Next, in lines 12 to 16 a menu for the system tray icon is created using item
objects with call-back to the function executed when the menu item was clicked on. And in the last two lines, the system tray is created and started using icon.run()
. When the Quit menu item is clicked, the on_quit()
function is called and ends the Python application.
Changing the Windows Display Resolution from the System Tray / Taskbar
With those two code snippets, you can create a Python application that helps you change the display resolution from the Windows system tray:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pystray
import pywintypes
import win32api
import win32con
from PIL import Image
from pystray import MenuItem as item
def on_set_resolution(width: int, height:int):
# adapted from Peter Wood: https://stackoverflow.com/a/54262365
devmode = pywintypes.DEVMODEType()
devmode.PelsWidth = width
devmode.PelsHeight = height
devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT
win32api.ChangeDisplaySettings(devmode, 0)
def on_quit():
icon.visible = False
icon.stop()
if __name__ == "__main__":
# adapted from Sebastin Ignacio Camilla Trinc: https://stackoverflow.com/a/48284187
image = Image.open("icon.png")
menu = (
item('2160p', lambda: on_set_resolution(3840, 2160)),
item('1440p', lambda: on_set_resolution(2560, 1440)),
item('Quit', on_quit)
)
icon = pystray.Icon("Resolution Switcher", image, "Resolution Switcher", menu)
icon.run()
The Python code for changing the Windows display resolution can now be found in the function on_set_resolution(width, height)
and is called via a lambda function from the menu items in lines 30 and 31. Copy this code into a file called resolution_switcher.py
.
When you run this Python script, an icon will appear in the system tray, and you can choose the resolution of your display.
Adding a Python Script to the Windows Startup
You don’t have to manually launch this Python application every time you boot your computer so let us add it to the Windows startup. First, you need a batch file that changes the directory to the folder the Python script is in, then the virtual environment has to be activated, and then the Python application can be launched using pythonw
such that no GUI shows up. Create a batch file resolution_switcher.bat
in the same folder the Python script is in and add the following (adapted) content:
1
cd "C:\Users\YOUR_USERNAME\FOLDER" & .\.venv\Scripts\activate & pythonw resolution_switcher.py
However, when you double-click on this batch file, the Command Prompt comes up and does not close until you end your Python application. Launching the Python application without any Command Prompt requires a Virtual Basic script. This Virtual Basic script also goes into the folder of the Python script and batch file and is called resolution_switcher.vbs
:
1
2
3
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "resolution_switcher.bat" & Chr(34), 0
Set WshShell = Nothing
This Virtual Basic script takes care of launching the Python system tray application without a Windows Command Prompt. Go ahead and double-click it to check it out.
A shortcut of the Virtual Basic script now has to be added to the Windows startup folder. To do so first right-click on the resolution_switcher.vbs
and choose Send to -> Desktop (create shortcut). Now you got a shortcut of resolution_switcher.vbs
on your Desktop. Now open Windows Run by pressing Win+R and enter and press Enter:
1
shell:startup
Now drag the shortcut to resolution_switcher.vbs
in opened Explorer window. And when you restart your computer, the Python Resolution Switcher application will be available in the system tray.
You can find all the code for this little Python application in my Github repository: https://github.com/k0nze/windows_resolution_switcher.
Make sure to get the free Python Cheat Sheets in my Gumroad shop. If you have any questions about this article, feel free to join our Discord community to ask them over there.