Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

turn monitor off and on

73 views
Skip to first unread message

Astan Chee

unread,
May 14, 2011, 2:08:44 AM5/14/11
to pytho...@python.org
Hi,
I'm trying to turn off my monitor, pause and then turn it on again.
I'm doing this in python 2.6 and windows xp. Here is my script so far
(that doesn't work):

import time
import win32gui
import win32con
import win32api

def turnOffMonitor():
SC_MONITORPOWER = 0xF170
win32gui.SendMessage(win32con.HWND_BROADCAST,
win32con.WM_SYSCOMMAND, SC_MONITORPOWER, 2)

def turnOnMonitor():
SC_MONITORPOWER = 0xF170
win32gui.SendMessage(win32con.HWND_BROADCAST,
win32con.WM_SYSCOMMAND, SC_MONITORPOWER, -1)

if __name__ == "__main__":
turnOffMonitor()
time.sleep(5)
turnOnMonitor()

For some reason, the script doesn't turn the monitor back on. What am
I doing wrong here or are there any other alternative?

harrismh777

unread,
May 14, 2011, 3:13:57 AM5/14/11
to
Astan Chee wrote:
> import time
> import win32gui
> import win32con
> import win32api
>
> def turnOffMonitor():
> SC_MONITORPOWER = 0xF170
> win32gui.SendMessage(win32con.HWND_BROADCAST,
> win32con.WM_SYSCOMMAND, SC_MONITORPOWER, 2)
>
> def turnOnMonitor():
> SC_MONITORPOWER = 0xF170
> win32gui.SendMessage(win32con.HWND_BROADCAST,
> win32con.WM_SYSCOMMAND, SC_MONITORPOWER, -1)
>
> if __name__ == "__main__":
> turnOffMonitor()
> time.sleep(5)
> turnOnMonitor()


What we need here is a matching script that turns off WinXP and leaves
it off too...

... ouch, <sorry, couldn't help myself> :)

lol
m harris

harrismh777

unread,
May 14, 2011, 3:20:55 AM5/14/11
to
harrismh777 wrote:
>
> def turnOnMonitor():
> SC_MONITORPOWER = 0xF170
> win32gui.SendMessage(win32con.HWND_BROADCAST,
> win32con.WM_SYSCOMMAND, SC_MONITORPOWER, -1)


I've never tried turning my monitor on/off without using my finger...
gonna have to play with this... wouldn't that be a great script kiddie
tool... just turn off all the windows monitors around the world... no
problem(s) after that...

... what happens if you try to turn it back on with your finger... ?

... I have another idea... while its off, try pushing the alt key on
your keyboard... or moving your mouse... (come back on ? )

kind regards,
m harris

harrismh777

unread,
May 14, 2011, 3:29:02 AM5/14/11
to
harrismh777 wrote:
>>
>> def turnOnMonitor():
>> SC_MONITORPOWER = 0xF170
>> win32gui.SendMessage(win32con.HWND_BROADCAST,
>> win32con.WM_SYSCOMMAND, SC_MONITORPOWER, -1)


Wonder what the equivalent of this is in Linux... ?

Steven D'Aprano

unread,
May 14, 2011, 3:43:12 AM5/14/11
to
On Sat, 14 May 2011 02:20:55 -0500, harrismh777 wrote:

> harrismh777 wrote:
>>
>> def turnOnMonitor():
>> SC_MONITORPOWER = 0xF170
>> win32gui.SendMessage(win32con.HWND_BROADCAST,
>> win32con.WM_SYSCOMMAND, SC_MONITORPOWER, -1)
>
>
> I've never tried turning my monitor on/off without using my finger...

You've never had your PC turn your monitor off after X minutes of
inactivity?


> gonna have to play with this... wouldn't that be a great script kiddie
> tool... just turn off all the windows monitors around the world... no
> problem(s) after that...
>
> ... what happens if you try to turn it back on with your finger... ?

Then you actually turn it off, at the power switch, instead of merely off
in software.


> ... I have another idea... while its off, try pushing the alt key on
> your keyboard... or moving your mouse... (come back on ? )

Presumably you would need to program something to watch for activity and
turn it back on. It would be somewhat embarrassing if you neglected to so
this...


--
Steven

Alexander Kapps

unread,
May 14, 2011, 3:59:07 AM5/14/11
to pytho...@python.org

Probably xset dpms force {on,off,...}

harrismh777

unread,
May 14, 2011, 4:05:11 AM5/14/11
to
Steven D'Aprano wrote:
>> I've never tried turning my monitor on/off without using my finger...

> You've never had your PC turn your monitor off after X minutes of
> inactivity?

I know you're being funny, but actually, no-- I don't.


That's a back-in-the-day thing... all of my monitors (and I only have
a couple now because I use VNC to get to the rest of my machines) are
flat panel LED, very low power, cool, and virtually indestructible. I
just leave them on... with beautiful screen savers running... which
aren't screen savers these days either... because nothing really
burns... more like perpetual light-candy eye-pleasers...

Sure, back in the day I configured my system to shutoff the CRT... but
honestly, I haven't had a CRT for almost a decade... you can still
shutdown the flat panel several different ways on the linux platform
based on timing if you want to... in BIOS, in the OS, in gnome, using
the xscreensaver, etc... but, no, I have never written a code routine
to shutoff the monitor (my finger always worked just fine). :)

I wrote a code routine one time just to open the drive bay door,
timer(), and then close the door... from across the network... until
my wife got wise to it, I would open her drive bay door over the
network... it was fun for a while, even after she got wise to it...


... till the morning I tried it and she had a cup of cocoa sitting on
the desk in front of the drive bay...


... there have been better days in my house. :)


Terry Reedy

unread,
May 14, 2011, 3:58:28 PM5/14/11
to pytho...@python.org
On 5/14/2011 3:20 AM, harrismh777 wrote:
> harrismh777 wrote:
>>
>> def turnOnMonitor():
>> SC_MONITORPOWER = 0xF170
>> win32gui.SendMessage(win32con.HWND_BROADCAST,
>> win32con.WM_SYSCOMMAND, SC_MONITORPOWER, -1)
>
>
> I've never tried turning my monitor on/off without using my finger...

The computer cannot turn off the monitor. It can only tell the graphics
card to turnoff the signal to the monitor. My monitor then displays 'No
signal detected' in a box and puts itself into a low-power state
awaiting a signal. Even if the monitor does not do that, a black screen
should use less power.

Why the turnon does not work, if indeed it does not (and the monitor is
not failing to respond to its resumption) is not a Python question. 2
and -1 as off and on parameters is a bit strange. The OP might try a
different api reference source or ask on a windows innards list.

--
Terry Jan Reedy

harrismh777

unread,
May 14, 2011, 8:25:39 PM5/14/11
to
Terry Reedy wrote:
> The computer cannot turn off the monitor.

... this was my point ;-)


Gregory Ewing

unread,
May 15, 2011, 8:00:01 PM5/15/11
to
Terry Reedy wrote:
> My monitor then displays 'No
> signal detected' in a box and puts itself into a low-power state
> awaiting a signal. Even if the monitor does not do that, a black screen
> should use less power.

I'm not so sure about that. If the monitor is an LCD and isn't
doing anything to reduce its own power usage, then the backlight
is still running and using just as much power, whether the screen
is black or not.

--
Greg

harrismh777

unread,
May 15, 2011, 9:48:37 PM5/15/11
to
Gregory Ewing wrote:
> I'm not so sure about that. If the monitor is an LCD and isn't
> doing anything to reduce its own power usage, then the backlight
> is still running and using just as much power, whether the screen
> is black or not.

Depends on dpmi. Some monitors turn off the backlight, and some don't.
My monitor(s) that are LCD turn off the backlight... and the downside of
that (very annoying) is that they have to 'warm up' again... making them
dim for the first few seconds of use...

An LED flat panel is a completely different animal, because there is no
backlight (no ccfl). They monitors don't use much power in the first
place, but they use less when they're blank, obviously, not producing light.

kind regards,
m harris

Gabriel Genellina

unread,
May 16, 2011, 5:29:00 AM5/16/11
to pytho...@python.org
En Sat, 14 May 2011 03:08:44 -0300, Astan Chee <astan...@gmail.com>
escribió:

> I'm trying to turn off my monitor, pause and then turn it on again.

> I'm doing this in python 2.6 and windows xp. Here is my script so far
> (that doesn't work):


>
> def turnOnMonitor():
> SC_MONITORPOWER = 0xF170
> win32gui.SendMessage(win32con.HWND_BROADCAST,
> win32con.WM_SYSCOMMAND, SC_MONITORPOWER, -1)
>

> For some reason, the script doesn't turn the monitor back on. What am
> I doing wrong here or are there any other alternative?

Your script worked fine for me, 2.6 and XP also. Perhaps your monitor
device driver is buggy or does not implement the required functionality.
Mine is from Philips.

--
Gabriel Genellina

Jack Krieger

unread,
May 20, 2011, 5:54:43 AM5/20/11
to pytho...@python.org
> def turnOffMonitor():
> SC_MONITORPOWER = 0xF170
> win32gui.SendMessage(win32con.HWND_BROADCAST,
> win32con.WM_SYSCOMMAND, SC_MONITORPOWER, 2)

This code does not return control to you, so programm still locked. In my opinion it due broadcasting message. But I do not know how to send message to monitor directly.

Message has been deleted
0 new messages