首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Python自动锁屏–window系统「建议收藏」

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

作者头像
全栈程序员站长
发布2022-09-13 19:01:35
发布2022-09-13 19:01:35
1.6K0
举报

大家好,又见面了,我是你们的朋友全栈君。

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

代码语言:javascript
复制
#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 如下

代码语言:javascript
复制
#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版本

代码语言:javascript
复制
#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

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/162761.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档