Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # NOTE: Windows users need to install pypiwin32 for this script to work:
- # Run CMD as administrator > pip install pypiwin32
- import platform
- def screenOff():
- system = platform.system()
- if system == 'Linux':
- import subprocess
- import os
- if os.environ.get('XDG_SESSION_TYPE') == 'x11':
- # NOTE: To prevent passworded lockscreen from appearing for XFCE:
- # Start > Power Manager > Security:
- # Automatically lock the session: Never
- # [ ] Lock screen when system is going to sleep
- # SOURCE: https://stackoverflow.com/questions/60851689/turn-screen-off-in-linux-via-python3
- # SOURCE: https://askubuntu.com/questions/38776/trigger-screensaver-off-monitor
- # SOURCE: https://ubuntuforums.org/showthread.php?t=1607724
- # SOURCE: https://wiki.archlinux.org/title/Display_Power_Management_Signaling
- subprocess.run(["xset", "dpms", "force", "suspend"])
- elif os.environ.get('XDG_SESSION_TYPE') == 'wayland':
- # NOTE: To prevent passworded lockscreen from appearing for KDE:
- # Start > Screen Locking:
- # Lock screen automatically: [ ] After [5 minutes]
- # [ ] After waking from sleep
- # SOURCE: https://askubuntu.com/questions/1316097/how-to-turn-off-the-monitor-via-command-on-wayland-kde-plasma
- subprocess.run(["kscreen-doctor", "--dpms", "off"])
- else:
- print('Could not determine the display server.')
- elif system == 'Windows':
- # pip install pypiwin32
- import win32con
- import win32gui
- # SOURCE: https://stackoverflow.com/questions/70965202/python-turn-screen-on-and-off-on-windows
- win32gui.PostMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
- else:
- print('Unknown OS detected:', system)
- # ----
- import time
- def countdownMessage(message, delaySeconds):
- while delaySeconds > 0:
- print('\r', message.format(delaySeconds), end='', flush=True)
- time.sleep(1)
- delaySeconds -= 1
- def main():
- # This delay is to give the mouse a chance to stop moving.
- # Mouse-wire tension + slippery feet can cause unintended mouse movement which will wake up the monitor prematurely.
- countdownMessage('Turning off monitor in {} seconds . . . ', 5)
- screenOff()
- return
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement