簡體   English   中英

Python/Windows 10 - 有沒有辦法防止 Windows 10 計算機在 Python 進入睡眠狀態?

[英]Python/Windows 10 - Is there a way to prevent a Windows 10 computer from going to sleep in Python?

有沒有辦法防止 Windows 10 計算機在 Python 進入休眠狀態?

與此處發布的問題基本相同,但對於 Windows 10: 防止 OS X 使用 Python 進入睡眠狀態?

我可以從 Python 調用 Windows 命令嗎?

如果有任何區別,我正在使用 Python 3.6。

對於將來看到這個的任何人,我強烈推薦 kbarnes3 的這個裝飾器: https://gist.github.com/kbarnes3/3fb7d353e9bdd3efccd5只需下載文件,將其放在腳本目錄中並按如下方式使用:

from powermanagement import long_running

@long_running
def your_function():
    #do stuff

your_function()

只要 your_function() 正在運行,電腦就不會關閉屏幕,因此 go 不會進入睡眠狀態。

它通過使用 ctypes 模塊和線程執行狀態來工作。 據我所知,這也是大多數媒體播放器等使用的方法。這是一個非常干凈的解決方案,因為您不必移動鼠標或按鍵。

這可能不是一個直接的答案也不是一種優雅的方式,但會奏效。 您可以使用 PyAutoGUI 按特定時間間隔單擊特定屏幕位置。 這將迫使你的電腦不休眠

import time
import pyautogui

# press ctrl+c to get out of the loop
while(True):
    pyautogui.click(x=100, y=200) # make sure nothing is there in the clicked location
    time.sleep(10)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM