鱼C论坛

 找回密码
 立即注册
查看: 1069|回复: 2

[已解决]自动关机,没有等待时间,请大师帮忙,谢谢

[复制链接]
发表于 2023-5-14 21:39:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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()
最佳答案
2023-5-14 22:02:57
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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-5-14 21:39:41 | 显示全部楼层
这段代码实现了一个定时关机的功能,并且在关机前提醒用户保存数据。如果用户在180秒内没有输入任何指令,程序将默认执行关机操作。如果需要让程序等待180秒后再执行默认操作,可以对计时线程函数timeout()进行修改。修改后的代码如下:

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))
        time.sleep(1) # 将计时器的时间间隔改为1秒

在原有代码的基础上,将计时器的时间间隔改为1秒即可。这样程序将会每隔1秒检查一次是否有用户输入,如果180秒内没有输入,则会执行默认操作。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-14 22:02:57 | 显示全部楼层    本楼为最佳答案   
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()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 05:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表