Setting monitor power state in python?

How can I send a monitor into/out-of a different power state (like sleep)?

Asked by: Owen384 | Posted: 24-09-2021

Answer 1

After looking at this article:

It appears you need to send a SendMessage call similar to:


Although, that is a VB version. What you're really after is the WinAPI call, I'm sure you can convert this bit to however you invoke WinAPI calls in Python. I hope this helps.

Answered by: Ned853 | Posted: 25-10-2021

Answer 2

import win32gui
import win32con

if argument == "on":
  win32gui.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND,
                       win32con.SC_MONITORPOWER, -1)

if argument == "off":
  win32gui.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND,
                       win32con.SC_MONITORPOWER, 2)

if argument == "sleep":
  win32gui.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND,
                       win32con.SC_MONITORPOWER, 1)

Answered by: Daryl235 | Posted: 25-10-2021

Similar questions

python - Setting the flags field of the IP header

I have a simple Python script that uses the socket module to send a UDP packet. The script works fine on my Windows box, but on my Ubuntu Linux PC the packet it sends is slightly different. On Windows the flags field in the IP header is zero, but using the same code on Linux created a packet with the flags field set to 4. I'd like to modify my script so it has consistent behavior on Windows and Linux. Is there a me...

python - Setting a colour scale in ipython

I am new to python and am having trouble finding the correct syntax to use. I want to plot some supernovae data onto a hammer projection. The data has coordinates alpha and beta. For each data point there is also a value delta describing a property of the SN. I would like to create a colour scale that ranges from min. value of delta to max. value of delta and goes from red to violet. This would mean that when I came to pl...

python - Setting an object in the Django cache API fails due to pickle error

I'm trying to manually set an object in the Django cache API but it fails (i think due to pickling?) The object is given to me by a third party, my code is: def index(request, template_name="mytemplate.htm"): user_list = cache.get("user_list_ds") if user_list is None: # this is the expensive bit I'm trying to cache # given to me by a third part user_list =

python - How to setting Camera options in Blender

i try to create car's type game, but when i choose camera view , a view is not real every building have miss shape its look like perspective view .So i finding how to config it

Error setting up thrift modules for python

I'm trying to set up thrift in order to incorporate with Cassandra, so when I ran the it out puts this message in command line running build running build_py running build_ext building 'thrift.protocol.fastbinary...

python - Why does setting this member in C fail?

I'm writing a Python wrapper for a C++ library, and I'm getting a really weird when trying to set a struct's field in C. If I have a struct like this: struct Thing { PyOBJECT_HEAD unsigned int val; }; And have two functions like this: static PyObject* Thing_GetBit(Thing* self, PyObject* args) { unsigned int mask; if(!PyArg_ParseTuple(args, "I", &a...

python - Setting custom SQL in django admin

I'm trying to set up a proxy model in django admin. It will represent a subset of the original model. The code from class MyManager(models.Manager): def get_query_set(self): return super(MyManager, self).get_query_set().filter(some_column='value') class MyModel(OrigModel): objects...

linux - setting ftp service via web with python

sorry if my english bad... anyone can tell me how to setting ftp service via web using python... i want my site can control the ftp service like sinology diskstation.. like enable/disable ftp service, control how many connection, add or remove user, etc. i use linux and python+django or just give me where to find the article..

python - Setting a default value for a field in a formset in Django

In a formset in Django, how do we set a default value for a field which needs a value from the http session? Since a session is required to get the value, we cannot set a default value in the model class itself. And I am not able to understand how to explicitly set the value in each form in the formset before saving in the view function. Setting the initial attribute in the construction of the FormSet would work bu...

python - Setting "cc" when sending e-mail from Django

Django 1.3 will add a "cc" argument to EmailMessage, which is excellent. How would one mimic this using Django 1.2? First, I tried this: headers = None if form.cleaned_data['cc_sender']: headers = {'Cc': sender} # `cc` argument added in Django 1.3 msg = EmailMultiAlternativ...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python