|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
自动关机,没有等待时间,直接进入默认,有什么办法让它等180秒,没有输入时,再设为默认,请大师帮忙,谢谢
import os
import sys
import threading
import time
import win32api
import win32con
def shutdown():
print('(1)定时关机\n(2)取消定时关机\n(3)立即关机\n(4)关机重启')
# 定义倒计时限制
time_limit = 180 # 单位为秒
b = None
# 定义计时线程函数
def timeout():
nonlocal b
for i in range(time_limit):
if b is not None:
return
if i % 60 == 0 and i > 0:
print("剩余时间:%d分钟" % (time_limit//60 - i//60))
win32api.MessageBox(win32con.NULL, u'还有3m关机,赶快保存一下!', u'温馨提醒', win32con.MB_OK)
timer_thread = threading.Thread(target=timeout)
timer_thread.start()
# 定义键盘监听线程函数
def key_press_listener():
nonlocal b
while True:
try:
if msvcrt.kbhit():
b = int(msvcrt.getch())
if b in range(1, 5):
break
else:
print('输入不正确,请重新输入.')
except ValueError:
print('输入不正确,请重新输入.')
time.sleep(0.01)
input_thread = threading.Thread(target=key_press_listener)
input_thread.start()
# 等待用户输入或计时线程超时
while True:
if not timer_thread.is_alive():
break
# 等待键盘监听线程结束
input_thread.join()
# 等待计时线程结束
timer_thread.join()
if not b or b < 1 or b > 4:
b = 5
if(b==1):
while True:
try:
time = int(input('请输入定时关机的时间(分钟):\n').strip())
break
except ValueError:
print('输入不正确,请重新输入.')
os.system('shutdown -s -t '+str(time*60))
print('设置成功!电脑将在%d分钟后自动关闭。'%time)
elif(b==2):
os.system('shutdown -a')
print('成功取消定时关机设置!')
elif(b==3):
print('确定要立即关机吗?[y/n]')
if(msvcrt.getch().decode().strip()=='y'):
os.system('shutdown -s -t 0')
elif(b==4):
print('确定要立即关机重启吗?[y/n]')
if(msvcrt.getch().decode().strip()=='y'):
os.system("shutdown -r -t 0")
else:
time = 30
os.system('shutdown -s -t '+str(time*60))
print('设置成功!电脑将在%d分钟后自动关闭。'%time)
if __name__ == '__main__':
shutdown()
- import os
- import sys
- import threading
- import time
- import win32api
- import win32con
- import msvcrt
- def shutdown():
- print('(1)定时关机\n(2)取消定时关机\n(3)立即关机\n(4)关机重启')
- # 定义倒计时限制
- time_limit = 180 # 增加了默认关机时间
- b = None
- # 定义计时线程函数
- def timeout():
- nonlocal b
- for i in range(time_limit):
- if b is not None: # 如果变量b不为None,则退出循环
- return
- if i % 60 == 0 and i > 0:
- print("剩余时间:%d分钟" % (time_limit//60 - i//60))
- win32api.MessageBox(win32con.NULL, u'还有3m关机,赶快保存一下!', u'温馨提醒', win32con.MB_OK)
-
- timer_thread = threading.Thread(target=timeout)
- timer_thread.start()
- # 定义键盘监听线程函数
- def key_press_listener():
- nonlocal b
- while True:
- try:
- if msvcrt.kbhit():
- b = int(msvcrt.getch())
- if b in range(1, 5):
- break
- if not timer_thread.is_alive(): # 如果计时线程结束了,就退出循环
- break
- except ValueError:
- print('输入不正确,请重新输入.')
- time.sleep(0.01)
- input_thread = threading.Thread(target=key_press_listener)
- input_thread.start()
- # 等待用户输入或计时线程超时
- while True:
- if not timer_thread.is_alive():
- break
- # 等待键盘监听线程结束
- input_thread.join()
- # 等待计时线程结束
- timer_thread.join()
- if not b or b < 1 or b > 4:
- b = 5
- if(b==1):
- while True:
- try:
- time = int(input('请输入定时关机的时间(分钟):\n').strip())
- break
- except ValueError:
- print('输入不正确,请重新输入.')
- os.system('shutdown -s -t '+str(time*60))
- print('设置成功!电脑将在%d分钟后自动关闭。'%time)
- elif(b==2):
- os.system('shutdown -a')
- print('成功取消定时关机设置!')
- elif(b==3):
- print('确定要立即关机吗?[y/n]')
- if(msvcrt.getch().decode().strip()=='y'):
- os.system('shutdown -s -t 0')
- elif(b==4):
- print('确定要立即关机重启吗?[y/n]')
- if(msvcrt.getch().decode().strip()=='y'):
- os.system("shutdown -r -t 0")
- else:
- time = 3 # 将默认关机时间设为180秒
- os.system('shutdown -s -t '+str(time*60))
- print('设置成功!电脑将在%d秒后自动关闭。'%time)
- if name == 'main':
- shutdown()
复制代码
|
|