2025年Python自动锁屏–window系统「建议收藏」

Python自动锁屏–window系统「建议收藏」天天面对着电脑敲代码 你是否忘记了保护视力了 眼睛的度数在上涨 镜片变厚 这是我们期望的么 今天有点空闲时间 写了个 Python 自动锁屏脚本 使用的是 Python 2 7 代码如下 coding utf8 import os import time locktime 你设置的锁屏周期 单位 s locktime 1 60 60 starttime

天天面对着电脑敲代码,你是否忘记了保护视力了,眼睛的度数在上涨,镜片变厚,这是我们期望的么?今天有点空闲时间,写了个Python自动锁屏脚本,使用的是Python 2.7,代码如下

#coding:utf8

import os
import time

#locktime你设置的锁屏周期(单位 : s)
locktime = 1 * 60 * 60
starttime = int(time.time())

def locakMonitor():
os.system('RunDll32.exe user32.dll,LockWorkStation')

def showWindowMsg(info,btnval,wintitle):
vbstrcommand = 'mshta vbscript:msgbox('+'"'+info+'"'+","+str(btnval) +","+'"'+wintitle+'"'+")(window.close)"
#print vbstrcommand
os.system(vbstrcommand)

def getInput():
while True:
input = raw_input('continue lock(y or n):')
lowerinput = input.lower()
if 'y' == lowerinput:
return True
elif 'n' == lowerinput:
return False
else:
print u'输入错误'
continue

if __name__ == '__main__':
while True:
nowtime = int(time.time())
time.sleep(1)
#os.system("mshta vbscript:msgbox("+"cccc" +",64,"+"ccccc"+")(window.close)");
print 'after ' + str(nowtime - starttime) + 's'
if(nowtime == starttime + locktime):
showWindowMsg('lock after 5s',0,'lockinfo')
print u'5秒后将锁屏,请注意保护视力....'
time.sleep(5)
locakMonitor()
if getInput() == True:
starttime = int(time.time())
continue
else:
print u'锁屏脚本结束'
break
#重置开始时间
starttime = int(time.time())

可以自己去设定锁屏周期,运行下脚本,自动锁屏

修改部分拼写错误,锁屏时间计算问题,窗口弹出不显示,直接发出声音

version2.0 如下

#coding:utf8

import os
import time
import winsound
import webbrowser

#locktime你设置的锁屏周期(单位 : s)
locktime = 1 * 15
starttime = int(time.time())

def lockMonitor():
os.system('RunDll32.exe user32.dll,LockWorkStation')

def showWindowMsg(info,btnval,wintitle):
vbstrcommand = 'mshta vbscript:msgbox('+'"'+info+'"'+","+str(btnval) +","+'"'+wintitle+'"'+")(window.close)"
#print vbstrcommand
os.system(vbstrcommand)

def getInput():
while True:
input = raw_input('continue lock(y or n):')
lowerinput = input.lower()
if 'y' == lowerinput:
return True
elif 'n' == lowerinput:
return False
else:
print u'输入错误'
continue

if __name__ == '__main__':
while True:
time.sleep(1)
nowtime = int(time.time())
#os.system("mshta vbscript:msgbox("+"cccc" +",64,"+"ccccc"+")(window.close)");
print 'running ' + str(nowtime - starttime) + 's'
if nowtime >= (starttime + locktime):
#窗口显示看不到,发出声音
#showWindowMsg('lock after 5s',0,'lockinfo')
print u'5秒后将锁屏,请注意保护视力....'
winsound.Beep(600,5000)
#webbrowser.open('C:/Users/chengdu/Desktop/FILE/1.html')
#time.sleep(5)
lockMonitor()
if getInput() == True:
starttime = int(time.time())
continue
else:
print u'锁屏脚本结束'
break

Python3版本

#coding:utf8

import os
import time
import winsound
import webbrowser

#locktime你设置的锁屏周期(单位 : s)
locktime = 1 * 15
starttime = int(time.time())

def lockMonitor():
os.system('RunDll32.exe user32.dll,LockWorkStation')

def showWindowMsg(info,btnval,wintitle):
vbstrcommand = 'mshta vbscript:msgbox('+'"'+info+'"'+","+str(btnval) +","+'"'+wintitle+'"'+")(window.close)"
#print vbstrcommand
os.system(vbstrcommand)

def getInput():
while True:
kinput = input('continue running(y or n):')
lowerinput = kinput.lower()
if 'y' == lowerinput:
return True
elif 'n' == lowerinput:
return False
else:
print (u'输入错误')
continue

if __name__ == '__main__':
while True:
time.sleep(1)
nowtime = int(time.time())
#os.system("mshta vbscript:msgbox("+"cccc" +",64,"+"ccccc"+")(window.close)");
print ('running ' + str(nowtime - starttime) + 's')
if nowtime >= (starttime + locktime):
#窗口显示看不到,发出声音
#showWindowMsg('lock after 5s',0,'lockinfo')
print (u'5秒后将锁屏,请注意保护视力....')
winsound.Beep(600,5000)
#webbrowser.open('C:/Users/chengdu/Desktop/FILE/1.html')
#time.sleep(5)
lockMonitor()
if getInput() == True:
starttime = int(time.time())
continue
else:
print (u'锁屏脚本结束')
break
编程小号
上一篇 2025-01-28 07:30
下一篇 2025-02-05 20:21

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/111708.html