Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Prevent the screen saver

Tags:

python

I need to run a code for hours, and the computer (Windows) I am working with has a (forced and unchangeable) screensaver policy. (It locks after 10 minutes). What can I do to prevent it? Is there any line of code to prevent that?

like image 865
Zeinab Sobhani Avatar asked Jul 26 '26 09:07

Zeinab Sobhani


2 Answers

This worked for me. I'll just leave it here so people can use it.

import ctypes

ctypes.windll.kernel32.SetThreadExecutionState(0x80000002) #this will prevent the screen saver or sleep. 

## your code and operations 

ctypes.windll.kernel32.SetThreadExecutionState(0x80000000) #set the setting back to normal
like image 148
Zeinab Sobhani Avatar answered Jul 28 '26 21:07

Zeinab Sobhani


This is a coded solution that you can place in your program (also works for Mac users):

pip3 install pyautogui

https://pypi.org/project/PyAutoGUI/ (Reference)

import pyautogui
import time

def mouse_move():
    while True:
        pyautogui.moveTo(100, 100, duration = 1) # move the mouse
        time.sleep(60) # Every 1 min 
        pyautogui.moveTo(50, 100, duration = 1) # move the mouse


mouse_move() 

Or, without the while loop, run it when required if your program is already within a while loop:

def mouse_move():
    pyautogui.moveTo(50, 100, duration = 1) # move the mouse

mouse_move()
like image 23
SentBackInTime Avatar answered Jul 28 '26 21:07

SentBackInTime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!