3

is there a way to control the screen/monitor with python, like for example turn it off an on again (comparable to the shortcut-buttons on laptop keyboards)?

Thank you!

user2304540
  • 101
  • 1
  • 2
  • 8

3 Answers3

7

Most modern laptops have a physical connection between the button and the monitor. For instance, my Dell precision i can boot the PC and dimm my display even before BIOS launches which tells me, it's wired via hardware connections.

With that sad, i can still emulate a few system-calls from within the OS to execute the task as well, not just as energy efficient as pressing the actual button.

Doing this requires you to (on windows) use pywin32 to connect to the appropriate system api's and execute a very specific command to do so, and it is tricky if you're not familar with System API's and calling the windows 32 functions.

Something along the lines of:

import os, win32com.client

def runScreensaver():
    strComputer = "."
    objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
    colItems = objSWbemServices.ExecQuery("Select * from Win32_Desktop")
    for objItem in colItems:
        if objItem.ScreenSaverExecutable:
            os.system(objItem.ScreenSaverExecutable + " /start")
            break

If you're on linux you can try to just execute os.system() one of the following:

Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • I'm using windows with python 32 bit and I downloaded and installed pywin32. Now if I run your code, I get an error saying: Traceback (most recent call last): File "C:/Python33/screensaver.py", line 1, in What else do I have to do? import os, win32com.client File "C:\Python33\lib\site-packages\win32com\__init__.py", line 5, in import win32api, sys, os ImportError: No module named 'win32api' What else do I do wrong? – user2304540 May 06 '13 at 16:54
  • yes I did...it still doesnt't work this way, but don't worry I realized that for my purpose the automatic build in sleep mode is good enough :) – user2304540 May 07 '13 at 20:20
  • 1
    @Torxed I think you are talking about `os.system()`? – Santosh Kumar Jul 28 '13 at 08:03
5

In python

    import win32gui
    import win32con

    #to turn off use :-
    win32gui.SendMessage(win32con.HWND_BROADCAST,
                     win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
    #turn on use :-
    win32gui.SendMessage(win32con.HWND_BROADCAST,
                     win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, -1)
Omar Zakaria
  • 51
  • 1
  • 4
0

You can make this in linux:

def set_brightness(brightness):
    if int(brightness) > 15:
        raise TypeError("Need int 0 < and > 15")
    elif int(brightness) < 0:
        raise TypeError("Need int 0 < and > 15")
    with open("/sys/devices/pci0000:00/0000:00:02.0/backlight/acpi_video0/brightness","w") as bright:
         bright.write(str(brightness))
         bright.close()
set_brightness(0) #Brightness 0-15