鱼C论坛

 找回密码
 立即注册
查看: 2040|回复: 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
  1. import os
  2. import sys
  3. import threading
  4. import time
  5. import win32api
  6. import win32con
  7. import msvcrt

  8. def shutdown():
  9. print('(1)定时关机\n(2)取消定时关机\n(3)立即关机\n(4)关机重启')

  10. # 定义倒计时限制
  11. time_limit = 180 # 增加了默认关机时间
  12. b = None

  13. # 定义计时线程函数
  14. def timeout():
  15.     nonlocal b
  16.     for i in range(time_limit):
  17.         if b is not None: # 如果变量b不为None,则退出循环
  18.             return
  19.         if i % 60 == 0 and i > 0:
  20.             print("剩余时间:%d分钟" % (time_limit//60 - i//60))
  21.     win32api.MessageBox(win32con.NULL, u'还有3m关机,赶快保存一下!', u'温馨提醒', win32con.MB_OK)
  22.    
  23. timer_thread = threading.Thread(target=timeout)
  24. timer_thread.start()

  25. # 定义键盘监听线程函数
  26. def key_press_listener():
  27.     nonlocal b
  28.     while True:
  29.         try:
  30.             if msvcrt.kbhit():
  31.                 b = int(msvcrt.getch())
  32.                 if b in range(1, 5):
  33.                     break
  34.             if not timer_thread.is_alive(): # 如果计时线程结束了,就退出循环
  35.                 break
  36.         except ValueError:
  37.             print('输入不正确,请重新输入.')
  38.         time.sleep(0.01)

  39. input_thread = threading.Thread(target=key_press_listener)
  40. input_thread.start()

  41. # 等待用户输入或计时线程超时
  42. while True:
  43.     if not timer_thread.is_alive():
  44.         break

  45. # 等待键盘监听线程结束
  46. input_thread.join()
  47. # 等待计时线程结束
  48. timer_thread.join()

  49. if not b or b < 1 or b > 4:
  50.     b = 5

  51. if(b==1):
  52.     while True:
  53.         try:
  54.             time = int(input('请输入定时关机的时间(分钟):\n').strip())
  55.             break
  56.         except ValueError:
  57.             print('输入不正确,请重新输入.')
  58.     os.system('shutdown -s -t '+str(time*60))
  59.     print('设置成功!电脑将在%d分钟后自动关闭。'%time)
  60. elif(b==2):
  61.     os.system('shutdown -a')
  62.     print('成功取消定时关机设置!')
  63. elif(b==3):
  64.     print('确定要立即关机吗?[y/n]')
  65.     if(msvcrt.getch().decode().strip()=='y'):
  66.         os.system('shutdown -s -t 0')
  67. elif(b==4):
  68.     print('确定要立即关机重启吗?[y/n]')
  69.     if(msvcrt.getch().decode().strip()=='y'):
  70.         os.system("shutdown -r -t 0")
  71. else:
  72.     time = 3 # 将默认关机时间设为180秒
  73.     os.system('shutdown -s -t '+str(time*60))
  74.     print('设置成功!电脑将在%d秒后自动关闭。'%time)
  75. if name == 'main':
  76. shutdown()
复制代码
小甲鱼最新课程 -> https://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秒内没有输入,则会执行默认操作。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-14 22:02:57 | 显示全部楼层    本楼为最佳答案   
  1. import os
  2. import sys
  3. import threading
  4. import time
  5. import win32api
  6. import win32con
  7. import msvcrt

  8. def shutdown():
  9. print('(1)定时关机\n(2)取消定时关机\n(3)立即关机\n(4)关机重启')

  10. # 定义倒计时限制
  11. time_limit = 180 # 增加了默认关机时间
  12. b = None

  13. # 定义计时线程函数
  14. def timeout():
  15.     nonlocal b
  16.     for i in range(time_limit):
  17.         if b is not None: # 如果变量b不为None,则退出循环
  18.             return
  19.         if i % 60 == 0 and i > 0:
  20.             print("剩余时间:%d分钟" % (time_limit//60 - i//60))
  21.     win32api.MessageBox(win32con.NULL, u'还有3m关机,赶快保存一下!', u'温馨提醒', win32con.MB_OK)
  22.    
  23. timer_thread = threading.Thread(target=timeout)
  24. timer_thread.start()

  25. # 定义键盘监听线程函数
  26. def key_press_listener():
  27.     nonlocal b
  28.     while True:
  29.         try:
  30.             if msvcrt.kbhit():
  31.                 b = int(msvcrt.getch())
  32.                 if b in range(1, 5):
  33.                     break
  34.             if not timer_thread.is_alive(): # 如果计时线程结束了,就退出循环
  35.                 break
  36.         except ValueError:
  37.             print('输入不正确,请重新输入.')
  38.         time.sleep(0.01)

  39. input_thread = threading.Thread(target=key_press_listener)
  40. input_thread.start()

  41. # 等待用户输入或计时线程超时
  42. while True:
  43.     if not timer_thread.is_alive():
  44.         break

  45. # 等待键盘监听线程结束
  46. input_thread.join()
  47. # 等待计时线程结束
  48. timer_thread.join()

  49. if not b or b < 1 or b > 4:
  50.     b = 5

  51. if(b==1):
  52.     while True:
  53.         try:
  54.             time = int(input('请输入定时关机的时间(分钟):\n').strip())
  55.             break
  56.         except ValueError:
  57.             print('输入不正确,请重新输入.')
  58.     os.system('shutdown -s -t '+str(time*60))
  59.     print('设置成功!电脑将在%d分钟后自动关闭。'%time)
  60. elif(b==2):
  61.     os.system('shutdown -a')
  62.     print('成功取消定时关机设置!')
  63. elif(b==3):
  64.     print('确定要立即关机吗?[y/n]')
  65.     if(msvcrt.getch().decode().strip()=='y'):
  66.         os.system('shutdown -s -t 0')
  67. elif(b==4):
  68.     print('确定要立即关机重启吗?[y/n]')
  69.     if(msvcrt.getch().decode().strip()=='y'):
  70.         os.system("shutdown -r -t 0")
  71. else:
  72.     time = 3 # 将默认关机时间设为180秒
  73.     os.system('shutdown -s -t '+str(time*60))
  74.     print('设置成功!电脑将在%d秒后自动关闭。'%time)
  75. if name == 'main':
  76. shutdown()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 19:17

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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