简体   繁体   中英

python how to quit screensaver

I have to quit from screensaver. I have an alarm clock (written in python-pygame) that after a minute, without input, goes in screensaver (blank). I have also a pir sensor that, when I pass my hand, it stops the alarm. Therefore, if I'm in blank screen, the screensaver is activated and the alarm sounds, I'm able to stop the alarm with the pir but the screen remains blank. so, I would ask if there is a command that I can put in the python script that can exit from screensaver when I move the hand near the pir. It also would be useful for looking at the time when the screensaver is activated. thanks

Assuming the program will keep running when the screensaver begins (it should, however, if it does not, look into creating a daemon or service that will run in the background even when the computer is asleep), there is a way to do this. My solution requires that the system is a windows system (which in all likelihood it is, though I would be more able to help if you had specified). This is exactly the kind of task the pywin32 library is built to perform. Accessing the wake-up code within the command line using subprocess or os modules can result in unexpected problems, so it seems that it may be best to generate a small movement of the mouse event to wake up the computer. Here is an example of code that will wake up a computer in screensaver:

import win32api
import random

def wakeup():
    win32api.SetCursorPos((random.choice(range(300)),random.choice(range(300))))

This will just move the mouse to a randomized location on the screen so that the computer wakes up.

As for checking the time, this is quite simple. Using the time module, you can get the system time with this command:

time.localtime(time.time())

Time.time() returns the time in seconds only, localtime will convert it into something easier to understand.

I used all the suggestions and I wrote the following code that it's really working:

if io.input(pir_pin):
    pygame.mouse.set_pos((random.choice(range(600)), random.choice(range(600))))
    alarm.just_die()
time.sleep(0.5)

when I move the hand I get the screen appear and, if the alarm is sounding, the alarm stops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM