Python自動鎖屏--window系統
阿新 • • 發佈:2018-11-11
天天面對著電腦敲程式碼,你是否忘記了保護視力了,眼睛的度數在上漲,鏡片變厚,這是我們期望的麼?今天有點空閒時間,寫了個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