Python自动锁屏--window系统

    天天面对着电脑敲代码,你是否忘记了保护视力了,眼睛的度数在上涨,镜片变厚,这是我们期望的么?今天有点空闲时间,写了个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

 

你可能感兴趣的:(后端,自动锁屏)