[英]Python turn screen on and off on Windows
直接进入问题,我试图在我的主程序中实现屏幕/显示/监视器关闭和打开功能。 我研究了一下,发现这个答案很有趣。 所以,尝试测试它。 简而言之,代码如下:
import time
import win32gui
import win32con
def ScreenOFF():
"""
Function to turn off the screen.
"""
return win32gui.SendMessage(win32con.HWND_BROADCAST,
win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
def ScreenON():
"""
Function to turn on the screen.
"""
return win32gui.SendMessage(win32con.HWND_BROADCAST,
win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, -1)
ScreenOFF()
time.sleep(5)
ScreenON()
time.sleep(5)
屏幕关闭工作得很好,但是在 function 上执行屏幕时,屏幕只打开一秒钟,然后又立即关闭。 我现在什至无法解释为什么会发生这种情况!
也尝试了这种更原始的方法,但这里也是同样的问题:
import time
import ctypes
def ScreenOFF():
"""
Function to turn off the screen.
"""
ctypes.windll.user32.SendMessageW(65535, 274, 61808, 2)
def ScreenON():
"""
Function to turn on the screen.
"""
ctypes.windll.user32.SendMessageW(65535, 274, 61808, -1)
ScreenOFF()
time.sleep(5)
ScreenON()
屏幕关闭上有 github 存储库,就像这个一样,但屏幕上没有!
请建议我是否对此有任何修复或其他更好的方法来打开/关闭屏幕?
尝试使用 Asyncio 而不是时间?
不确定这是否可行,但如果不行,请回复此问题,我会继续努力。
编辑:Nvm asyncio 只能与 async 一起使用。 让我做一些测试。
另一个编辑:对不起,但我无法弄清楚。 也许加入 python 光盘?
不是一个真正解决它一打开就打开的问题,但我找到了一种让它不会关闭的方法。
不是一个很好的方法,但它有效
import time
import ctypes
import win32api, win32con
def screen_off():
ctypes.windll.user32.SendMessageW(65535, 274, 61808, 2)
def screen_on():
ctypes.windll.user32.SendMessageW(65535, 274, 61808, -1)
move_cursor()
def move_cursor():
x, y = (0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, x, y)
screen_off()
time.sleep(3)
screen_on()
如果您移动 cursor 或键入屏幕将停留的内容,那么
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.