0

I'm creating a python program that is supposed to streamline the process of setting up a computer. I want this python program to change the screen resolution of the computer and scaling of it. I'm not sure what the best approach is however, or how to approach it.

I've tried using an example pywin32 program, but it only outputted an array of resolution sizes

1

1 Answer 1

4

I had a look how to change screen resolution using C++ and then translated it to Python:

import win32api
import win32con
import pywintypes

devmode = pywintypes.DEVMODEType()

devmode.PelsWidth = 1366
devmode.PelsHeight = 768

devmode.Fields = win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT

win32api.ChangeDisplaySettings(devmode, 0)

We needed a DEVMODE object to pass to the ChangeDisplaySettings function. The pywintypes module which is also part of pywin32 has a function to create objects of type DEVMODE.

We then set the PelsWidth and PelsHeight fields and also the Fields field to tell the API which field's values we want to use.

To change back to the previous resolution, simply call:

win32api.ChangeDisplaySettings(None, 0)

Thanks for asking the question. I've learned something.

Sign up to request clarification or add additional context in comments.

4 Comments

what is the purpose of 0 in the line "win32api.ChangeDisplaySettings(None, 0)"
@BrandonChiu the linked documentation explains the flag means: "The graphics mode for the current screen will be changed dynamically". However, I don't know what that means. Looking at the other possible values for the flag, it seems the other options are to store the value in the registry, test whether settings are valid, only apply on restart, and change which is the primary screen, etc. So I imagine 0 really means "don't change anything else and make it temporary".
In the documentation it notes , that "CDS_GLOBAL" can be set as one of the flags.I tried replacing 0 with that flag, and pycharm throws me an unresolved reference. Is there an additional import I should be doing and/or process? Thanks
@BrandonChiu CDS_GLOBAL is defined in the same place as DM_PELSWIDTH and DM_PELSHEIGHT in win32con. Its value is 8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.