Advertisement
FocusedWolf

Python: MonitorOff

May 13th, 2024 (edited)
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # NOTE: Windows users need to install pypiwin32 for this script to work:
  4. #       Run CMD as administrator > pip install pypiwin32
  5.  
  6. import platform
  7. def screenOff():
  8.     system = platform.system()
  9.     if system == 'Linux':
  10.         import subprocess
  11.         import os
  12.         if os.environ.get('XDG_SESSION_TYPE') == 'x11':
  13.             # NOTE: To prevent passworded lockscreen from appearing for XFCE:
  14.             #       Start > Power Manager > Security:
  15.             #           Automatically lock the session: Never
  16.             #           [ ] Lock screen when system is going to sleep
  17.  
  18.             # SOURCE: https://stackoverflow.com/questions/60851689/turn-screen-off-in-linux-via-python3
  19.             # SOURCE: https://askubuntu.com/questions/38776/trigger-screensaver-off-monitor
  20.             # SOURCE: https://ubuntuforums.org/showthread.php?t=1607724
  21.             # SOURCE: https://wiki.archlinux.org/title/Display_Power_Management_Signaling
  22.             subprocess.run(["xset", "dpms", "force", "suspend"])
  23.         elif os.environ.get('XDG_SESSION_TYPE') == 'wayland':
  24.             # NOTE: To prevent passworded lockscreen from appearing for KDE:
  25.             #       Start > Screen Locking:
  26.             #           Lock screen automatically: [ ] After [5 minutes]
  27.             #                                      [ ] After waking from sleep
  28.  
  29.             # SOURCE: https://askubuntu.com/questions/1316097/how-to-turn-off-the-monitor-via-command-on-wayland-kde-plasma
  30.             subprocess.run(["kscreen-doctor", "--dpms", "off"])
  31.         else:
  32.             print('Could not determine the display server.')
  33.     elif system == 'Windows':
  34.         # pip install pypiwin32
  35.         import win32con
  36.         import win32gui
  37.         # SOURCE: https://stackoverflow.com/questions/70965202/python-turn-screen-on-and-off-on-windows
  38.         win32gui.PostMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
  39.     else:
  40.         print('Unknown OS detected:', system)
  41.  
  42. # ----
  43.  
  44. import time
  45. def countdownMessage(message, delaySeconds):
  46.     while delaySeconds > 0:
  47.         print('\r', message.format(delaySeconds), end='', flush=True)
  48.         time.sleep(1)
  49.         delaySeconds -= 1
  50.  
  51. def main():
  52.     # This delay is to give the mouse a chance to stop moving.
  53.     # Mouse-wire tension + slippery feet can cause unintended mouse movement which will wake up the monitor prematurely.
  54.     countdownMessage('Turning off monitor in {} seconds . . . ', 5)
  55.     screenOff()
  56.     return
  57.  
  58. if __name__ == '__main__':
  59.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement