• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    基于PyQt5实现的Windows定时关机工具

    发布者: 福建二哥 | 发布时间: 2025-6-17 08:05| 查看数: 263| 评论数: 0|帖子模式

    概述

    在日常使用电脑的过程中,我们经常会遇到需要定时关机的场景,比如:

    • 夜间下载文件,想让电脑在任务完成后自动关机。
    • 长时间运行的程序,需要在某个时间点关闭系统。
    • 限制电脑使用时间,避免长时间占用资源。
    虽然 Windows 自带
    1. shutdown
    复制代码
    命令可以定时关机,但操作方式较为繁琐,缺乏可视化界面。因此,本篇文章将带大家实现一个基于 PyQt5 的 Windows 定时关机工具,支持定时或延时关机、重启、注销,并提供系统托盘功能,方便随时管理关机任务。

    功能介绍

    本工具主要具备以下功能:
    定时关机 —— 设定具体时间,到点自动关机。
    延时关机 —— 设置倒计时,倒计时结束后自动关机。
    重启 & 注销 —— 除关机外,还可执行系统重启和注销操作。
    取消操作 —— 关机前可随时取消,避免误操作。
    系统托盘支持 —— 运行后最小化到系统托盘,不影响日常操作。
    人性化提示 —— 关机前弹出提醒,避免突发关机。

    代码实现


    1. 安装依赖

    在运行代码之前,我们需要先安装 PyQt5 库:
    1. pip install PyQt5 pyqt5-tools
    复制代码
    2. 代码编写

    以下是完整的代码实现:
    1. import sys
    2. import os
    3. import time
    4. from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QTimeEdit, QSystemTrayIcon, QMenu, QAction
    5. from PyQt5.QtGui import QIcon
    6. from PyQt5.QtCore import QTimer, QTime

    7. class ShutdownApp(QWidget):
    8.     def __init__(self):
    9.         super().__init__()
    10.         self.initUI()

    11.     def initUI(self):
    12.         self.setWindowTitle('Windows 定时关机工具')
    13.         self.setGeometry(600, 300, 300, 200)
    14.         
    15.         self.label = QLabel('请选择关机时间:', self)
    16.         self.timeEdit = QTimeEdit(self)
    17.         self.timeEdit.setDisplayFormat("HH:mm")
    18.         
    19.         self.startButton = QPushButton('设置关机', self)
    20.         self.startButton.clicked.connect(self.scheduleShutdown)
    21.         
    22.         self.cancelButton = QPushButton('取消关机', self)
    23.         self.cancelButton.clicked.connect(self.cancelShutdown)
    24.         
    25.         layout = QVBoxLayout()
    26.         layout.addWidget(self.label)
    27.         layout.addWidget(self.timeEdit)
    28.         layout.addWidget(self.startButton)
    29.         layout.addWidget(self.cancelButton)
    30.         
    31.         self.setLayout(layout)
    32.         
    33.         # 托盘功能
    34.         self.trayIcon = QSystemTrayIcon(QIcon("icon.png"), self)
    35.         trayMenu = QMenu()
    36.         exitAction = QAction("退出", self)
    37.         exitAction.triggered.connect(self.close)
    38.         trayMenu.addAction(exitAction)
    39.         self.trayIcon.setContextMenu(trayMenu)
    40.         self.trayIcon.show()

    41.     def scheduleShutdown(self):
    42.         shutdown_time = self.timeEdit.time()
    43.         current_time = QTime.currentTime()
    44.         seconds = current_time.secsTo(shutdown_time)
    45.         
    46.         if seconds <= 0:
    47.             self.label.setText("请选择一个未来的时间!")
    48.             return
    49.         
    50.         self.label.setText(f"关机已设置,将在 {shutdown_time.toString()} 执行")
    51.         os.system(f'shutdown -s -t {seconds}')

    52.     def cancelShutdown(self):
    53.         os.system('shutdown -a')
    54.         self.label.setText("关机已取消!")

    55. if __name__ == '__main__':
    56.     app = QApplication(sys.argv)
    57.     ex = ShutdownApp()
    58.     ex.show()
    59.     sys.exit(app.exec_())
    复制代码
    功能使用


    1. 运行软件
    1. python shutdown_tool.py
    复制代码
    2. 设置定时关机


    • 选择时间
    • 点击 “设置关机”
    • 程序将计算剩余时间,并执行关机命令

    3. 取消关机


    • 如果想取消定时关机,点击 “取消关机” 按钮
    • 也可以手动在命令行执行:
    1. shutdown -a
    复制代码
    4. 系统托盘


    • 运行后可最小化到托盘
    • 右键点击托盘图标可 退出应用

    技术要点解析


    关机命令

    Windows 提供
    1. shutdown
    复制代码
    命令来执行关机任务:

    • 定时关机
    1. shutdown -s -t 秒数
    复制代码

    • 取消关机
    1. shutdown -a
    复制代码
    计算关机时间

    我们使用
    1. QTime
    复制代码
    计算当前时间到设定时间的 秒数,避免时间计算错误:
    1. seconds = current_time.secsTo(shutdown_time)
    复制代码
    托盘图标支持

    使用
    1. QSystemTrayIcon
    复制代码
    实现最小化到托盘:
    1. self.trayIcon = QSystemTrayIcon(QIcon("icon.png"), self)
    复制代码
    这样即使窗口关闭,应用仍能在后台运行。

    运行效果







    相关源码
    1. import os
    2. import sys
    3. import time
    4. import configparser
    5. import win32api
    6. import win32con
    7. from datetime import datetime, timedelta
    8. from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
    9.                             QGroupBox, QRadioButton, QDateTimeEdit, QLabel, QPushButton,
    10.                             QCheckBox, QSystemTrayIcon, QMenu, QMessageBox, QSpacerItem,
    11.                             QSizePolicy, QFrame)
    12. from PyQt5.QtCore import Qt, QTimer, QDateTime, QTime, QSize, QSharedMemory
    13. from PyQt5.QtGui import QIcon, QFont, QPalette, QColor

    14. def resource_path(relative_path):
    15.     """ 获取资源的绝对路径,适用于开发环境和PyInstaller单文件模式 """
    16.     if hasattr(sys, '_MEIPASS'):
    17.         # PyInstaller创建的临时文件夹
    18.         return os.path.join(sys._MEIPASS, relative_path)
    19.     return os.path.join(os.path.abspath('.'), relative_path)

    20. class ShutdownApp(QMainWindow):
    21.     def __init__(self):
    22.         super().__init__()
    23.         self.task_running = False
    24.         self.config_file = os.path.join(os.getenv('APPDATA'), 'shutdown_config.ini')
    25.         self.first_show = True  # 用于跟踪是否是第一次显示
    26.         
    27.         self.setup_ui_style()
    28.         self.init_ui()
    29.         self.load_config()
    30.         
    31.         # 系统托盘
    32.         self.tray_icon = QSystemTrayIcon(self)
    33.         self.tray_icon.setIcon(QIcon(resource_path("icon.ico")))
    34.         self.tray_icon.setToolTip("定时关机")
    35.         self.tray_icon.activated.connect(self.tray_icon_activated)
    36.         
    37.         # 托盘菜单
    38.         self.tray_menu = QMenu()
    39.         self.show_action = self.tray_menu.addAction("显示")
    40.         self.exit_action = self.tray_menu.addAction("退出")
    41.         self.show_action.triggered.connect(self.show_normal)
    42.         self.exit_action.triggered.connect(self.confirm_exit)
    43.         self.tray_icon.setContextMenu(self.tray_menu)
    44.         self.tray_icon.show()  # 确保托盘图标显示
    45.         
    46.         # 显示当前时间
    47.         self.timer = QTimer(self)
    48.         self.timer.timeout.connect(self.update_current_time)
    49.         self.timer.start(1000)
    50.         
    51.         # 剩余时间计时器
    52.         self.countdown_timer = QTimer(self)
    53.         self.countdown_timer.timeout.connect(self.update_remaining_time)
    54.    
    55.     def setup_ui_style(self):
    56.         """设置全局UI样式"""
    57.         self.setStyleSheet("""
    58.             QMainWindow {
    59.                 background-color: #f5f5f5;
    60.             }
    61.             QGroupBox {
    62.                 border: 1px solid #ccc;
    63.                 border-radius: 4px;
    64.                 margin-top: 10px;
    65.                 padding-top: 15px;
    66.                 font-weight: bold;
    67.                 color: #333;
    68.             }
    69.             QGroupBox::title {
    70.                 subcontrol-origin: margin;
    71.                 left: 10px;
    72.                 padding: 0 3px;
    73.             }
    74.             QRadioButton, QCheckBox {
    75.                 color: #444;
    76.             }
    77.             QPushButton {
    78.                 background-color: #4CAF50;
    79.                 color: white;
    80.                 border: none;
    81.                 padding: 8px 16px;
    82.                 border-radius: 4px;
    83.                 min-width: 80px;
    84.             }
    85.             QPushButton:hover {
    86.                 background-color: #45a049;
    87.             }
    88.             QPushButton:disabled {
    89.                 background-color: #cccccc;
    90.             }
    91.             QPushButton#cancel_btn {
    92.                 background-color: #f44336;
    93.             }
    94.             QPushButton#cancel_btn:hover {
    95.                 background-color: #d32f2f;
    96.             }
    97.             QDateTimeEdit {
    98.                 padding: 5px;
    99.                 border: 1px solid #ddd;
    100.                 border-radius: 4px;
    101.             }
    102.             QLabel#current_time_label {
    103.                 font-size: 16px;
    104.                 color: #333;
    105.                 padding: 5px;
    106.                 background-color: #e9f5e9;
    107.                 border-radius: 4px;
    108.             }
    109.             QLabel#remaining_time_label {
    110.                 font-size: 16px;
    111.                 color: #d32f2f;
    112.                 font-weight: bold;
    113.                 padding: 5px;
    114.                 background-color: #f9e9e9;
    115.                 border-radius: 4px;
    116.             }
    117.         """)
    118.    
    119.     def init_ui(self):
    120.         self.setWindowTitle("定时关机")
    121.         self.setWindowIcon(QIcon(resource_path("icon.ico")))
    122.         self.resize(300, 440)
    123.         
    124.         # 主窗口布局
    125.         main_widget = QWidget()
    126.         self.setCentralWidget(main_widget)
    127.         layout = QVBoxLayout(main_widget)
    128.         layout.setContentsMargins(12, 12, 12, 12)
    129.         layout.setSpacing(10)
    130.         
    131.         # 当前时间显示
    132.         self.current_time_label = QLabel()
    133.         self.current_time_label.setAlignment(Qt.AlignCenter)
    134.         self.current_time_label.setObjectName("current_time_label")
    135.         layout.addWidget(self.current_time_label)
    136.         
    137.         # 添加分隔线
    138.         line = QFrame()
    139.         line.setFrameShape(QFrame.HLine)
    140.         line.setFrameShadow(QFrame.Sunken)
    141.         layout.addWidget(line)
    142.         
    143.         # 定时/延时选择
    144.         time_type_group = QGroupBox("时间类型")
    145.         time_type_layout = QHBoxLayout()
    146.         time_type_layout.setContentsMargins(10, 15, 10, 10)
    147.         
    148.         self.fixed_time_radio = QRadioButton("定时关机")
    149.         self.delay_time_radio = QRadioButton("延时关机")
    150.         self.fixed_time_radio.setChecked(True)
    151.         
    152.         time_type_layout.addWidget(self.fixed_time_radio)
    153.         time_type_layout.addWidget(self.delay_time_radio)
    154.         time_type_group.setLayout(time_type_layout)
    155.         layout.addWidget(time_type_group)
    156.         
    157.         # 定时时间选择
    158.         self.fixed_datetime = QDateTimeEdit()
    159.         self.fixed_datetime.setDisplayFormat("yyyy-MM-dd HH:mm:ss")
    160.         self.fixed_datetime.setDateTime(QDateTime.currentDateTime())
    161.         self.fixed_datetime.setCalendarPopup(True)
    162.         layout.addWidget(self.fixed_datetime)
    163.         
    164.         # 延时时间选择
    165.         self.delay_datetime = QDateTimeEdit()
    166.         self.delay_datetime.setDisplayFormat("HH:mm:ss")
    167.         self.delay_datetime.setTime(QTime(0, 0, 0))
    168.         self.delay_datetime.setVisible(False)
    169.         layout.addWidget(self.delay_datetime)
    170.         
    171.         # 连接信号
    172.         self.fixed_time_radio.toggled.connect(self.on_time_type_changed)
    173.         
    174.         # 操作类型
    175.         action_group = QGroupBox("操作类型")
    176.         action_layout = QHBoxLayout()
    177.         action_layout.setContentsMargins(10, 15, 10, 10)
    178.         
    179.         self.shutdown_radio = QRadioButton("关机")
    180.         self.restart_radio = QRadioButton("重启")
    181.         self.logoff_radio = QRadioButton("注销")
    182.         self.shutdown_radio.setChecked(True)
    183.         
    184.         action_layout.addWidget(self.shutdown_radio)
    185.         action_layout.addWidget(self.restart_radio)
    186.         action_layout.addWidget(self.logoff_radio)
    187.         action_group.setLayout(action_layout)
    188.         layout.addWidget(action_group)
    189.         
    190.         # 选项
    191.         options_group = QGroupBox("选项")
    192.         options_layout = QVBoxLayout()
    193.         options_layout.setContentsMargins(10, 15, 10, 10)
    194.         
    195.         self.auto_start_check = QCheckBox("随系统启动")
    196.         self.loop_exec_check = QCheckBox("循环执行")
    197.         
    198.         options_layout.addWidget(self.auto_start_check)
    199.         options_layout.addWidget(self.loop_exec_check)
    200.         options_group.setLayout(options_layout)
    201.         layout.addWidget(options_group)
    202.         
    203.         # 剩余时间显示
    204.         self.remaining_time_label = QLabel()
    205.         self.remaining_time_label.setAlignment(Qt.AlignCenter)
    206.         self.remaining_time_label.setObjectName("remaining_time_label")
    207.         layout.addWidget(self.remaining_time_label)
    208.         
    209.         # 按钮布局
    210.         button_layout = QHBoxLayout()
    211.         button_layout.setContentsMargins(0, 10, 0, 0)
    212.         button_layout.setSpacing(15)
    213.         
    214.         self.save_btn = QPushButton("保存设置")
    215.         self.cancel_btn = QPushButton("取消")
    216.         self.cancel_btn.setObjectName("cancel_btn")
    217.         self.cancel_btn.setEnabled(False)
    218.         
    219.         self.save_btn.clicked.connect(self.save_config)
    220.         self.cancel_btn.clicked.connect(self.cancel_task)
    221.         
    222.         button_layout.addWidget(self.save_btn)
    223.         button_layout.addWidget(self.cancel_btn)
    224.         layout.addLayout(button_layout)
    225.         
    226.         # 添加弹簧使布局更紧凑
    227.         layout.addSpacerItem(QSpacerItem(20, 10, QSizePolicy.Minimum, QSizePolicy.Expanding))
    228.    
    229.     def on_time_type_changed(self, checked):
    230.         self.fixed_datetime.setVisible(checked)
    231.         self.delay_datetime.setVisible(not checked)
    232.         
    233.     def update_current_time(self):
    234.         current_time = datetime.now().strftime("%Y年%m月%d日 %H:%M:%S")
    235.         self.current_time_label.setText(f"当前时间: {current_time}")
    236.         
    237.     def save_config(self):
    238.         config = configparser.ConfigParser()
    239.         config['task'] = {
    240.             'time_type': 'fixed' if self.fixed_time_radio.isChecked() else 'delay',
    241.             'time': self.fixed_datetime.dateTime().toString("HHmmss") if self.fixed_time_radio.isChecked() else self.delay_datetime.time().toString("HHmmss"),
    242.             'execute_type': 'shutdown' if self.shutdown_radio.isChecked() else 'restart' if self.restart_radio.isChecked() else 'logoff',
    243.             'auto_start': '1' if self.auto_start_check.isChecked() else '0',
    244.             'task_circ': '1' if self.loop_exec_check.isChecked() else '0'
    245.         }
    246.         
    247.         with open(self.config_file, 'w') as f:
    248.             config.write(f)
    249.             
    250.         self.set_auto_start(self.auto_start_check.isChecked())
    251.         self.cancel_btn.setEnabled(True)  # 保存后启用取消按钮
    252.         self.start_task()
    253.         
    254.     def load_config(self):
    255.         if not os.path.exists(self.config_file):
    256.             return
    257.             
    258.         config = configparser.ConfigParser()
    259.         config.read(self.config_file)
    260.         
    261.         if 'task' in config:
    262.             task_config = config['task']
    263.             
    264.             # 时间类型
    265.             if task_config.get('time_type', 'fixed') == 'fixed':
    266.                 self.fixed_time_radio.setChecked(True)
    267.                 time_str = task_config.get('time', '000000')
    268.                 qtime = QTime.fromString(time_str, "HHmmss")
    269.                 current_date = QDateTime.currentDateTime()
    270.                 self.fixed_datetime.setDateTime(QDateTime(current_date.date(), qtime))
    271.             else:
    272.                 self.delay_time_radio.setChecked(True)
    273.                 time_str = task_config.get('time', '000000')
    274.                 qtime = QTime.fromString(time_str, "HHmmss")
    275.                 self.delay_datetime.setTime(qtime)
    276.                
    277.             # 操作类型
    278.             execute_type = task_config.get('execute_type', 'shutdown')
    279.             if execute_type == 'shutdown':
    280.                 self.shutdown_radio.setChecked(True)
    281.             elif execute_type == 'restart':
    282.                 self.restart_radio.setChecked(True)
    283.             else:
    284.                 self.logoff_radio.setChecked(True)
    285.                
    286.             # 选项
    287.             self.auto_start_check.setChecked(task_config.get('auto_start', '0') == '1')
    288.             self.loop_exec_check.setChecked(task_config.get('task_circ', '0') == '1')
    289.             
    290.             if self.loop_exec_check.isChecked():
    291.                 self.cancel_btn.setEnabled(True)
    292.                 self.start_task()
    293.                
    294.     def set_auto_start(self, enable):
    295.         key = win32con.HKEY_CURRENT_USER
    296.         sub_key = r"Software\Microsoft\Windows\CurrentVersion\Run"
    297.         
    298.         try:
    299.             reg_key = win32api.RegOpenKey(key, sub_key, 0, win32con.KEY_ALL_ACCESS)
    300.             if enable:
    301.                 win32api.RegSetValueEx(reg_key, "定时关机", 0, win32con.REG_SZ, sys.executable)
    302.             else:
    303.                 try:
    304.                     win32api.RegDeleteValue(reg_key, "定时关机")
    305.                 except:
    306.                     pass
    307.             win32api.RegCloseKey(reg_key)
    308.         except Exception as e:
    309.             QMessageBox.warning(self, "警告", f"设置自启动失败: {str(e)}")
    310.             
    311.     def start_task(self):
    312.         if self.task_running:
    313.             return
    314.             
    315.         self.task_running = True
    316.         self.toggle_components(True)
    317.         
    318.         if self.fixed_time_radio.isChecked():
    319.             target_time = self.fixed_datetime.dateTime().toPyDateTime()
    320.             now = datetime.now()
    321.             
    322.             if target_time < now:
    323.                 target_time += timedelta(days=1)
    324.                
    325.             self.target_time = target_time
    326.         else:
    327.             delay = self.delay_datetime.time()
    328.             self.target_time = datetime.now() + timedelta(
    329.                 hours=delay.hour(),
    330.                 minutes=delay.minute(),
    331.                 seconds=delay.second()
    332.             )
    333.             
    334.         self.countdown_timer.start(1000)
    335.         self.update_remaining_time()
    336.         
    337.     def update_remaining_time(self):
    338.         now = datetime.now()
    339.         remaining = self.target_time - now
    340.         
    341.         if remaining.total_seconds() <= 0:
    342.             self.execute_action()
    343.             if self.loop_exec_check.isChecked():
    344.                 self.start_task()
    345.             else:
    346.                 self.cancel_task()
    347.             return
    348.             
    349.         hours, remainder = divmod(int(remaining.total_seconds()), 3600)
    350.         minutes, seconds = divmod(remainder, 60)
    351.         remaining_str = f"{hours}小时{minutes}分{seconds}秒"
    352.         self.remaining_time_label.setText(f"剩余时间: {remaining_str}")
    353.         
    354.         # 更新托盘提示
    355.         self.tray_icon.setToolTip(f"定时关机\n剩余时间: {remaining_str}")
    356.         
    357.     def execute_action(self):
    358.         if self.shutdown_radio.isChecked():
    359.             os.system("shutdown -s -t 0")
    360.         elif self.restart_radio.isChecked():
    361.             os.system("shutdown -r -t 0")
    362.         else:
    363.             os.system("shutdown -l")
    364.             
    365.     def cancel_task(self):
    366.         """取消定时任务"""
    367.         if not self.task_running:
    368.             QMessageBox.warning(self, "警告", "没有正在运行的任务")
    369.             return
    370.             
    371.         reply = QMessageBox.question(
    372.             self,
    373.             "确认",
    374.             "确定要取消定时任务吗?",
    375.             QMessageBox.Yes | QMessageBox.No
    376.         )
    377.         
    378.         if reply == QMessageBox.Yes:
    379.             self.task_running = False
    380.             self.countdown_timer.stop()
    381.             self.remaining_time_label.setText("")
    382.             self.tray_icon.setToolTip("定时关机")
    383.             self.toggle_components(False)
    384.             self.cancel_btn.setEnabled(False)
    385.             
    386.             # 显示取消成功的提示
    387.             QMessageBox.information(self, "提示", "定时任务已取消", QMessageBox.Ok)
    388.         
    389.     def toggle_components(self, disabled):
    390.         self.fixed_time_radio.setDisabled(disabled)
    391.         self.delay_time_radio.setDisabled(disabled)
    392.         self.fixed_datetime.setDisabled(disabled)
    393.         self.delay_datetime.setDisabled(disabled)
    394.         self.shutdown_radio.setDisabled(disabled)
    395.         self.restart_radio.setDisabled(disabled)
    396.         self.logoff_radio.setDisabled(disabled)
    397.         self.auto_start_check.setDisabled(disabled)
    398.         self.loop_exec_check.setDisabled(disabled)
    399.         self.save_btn.setDisabled(disabled)
    400.         
    401.     def tray_icon_activated(self, reason):
    402.         if reason == QSystemTrayIcon.DoubleClick:
    403.             self.show_normal()
    404.             
    405.     def show_normal(self):
    406.         self.show()
    407.         self.setWindowState(self.windowState() & ~Qt.WindowMinimized)
    408.         self.activateWindow()
    409.         self.raise_()
    410.         
    411.     def closeEvent(self, event):
    412.         """重写关闭事件,改为最小化到托盘"""
    413.         if self.isVisible():
    414.             self.hide()
    415.             self.tray_icon.show()  # 确保托盘图标显示
    416.             if not self.first_show:  # 第一次启动时不显示消息
    417.                 self.tray_icon.showMessage(
    418.                     "定时关机",
    419.                     "程序已最小化到托盘",
    420.                     QSystemTrayIcon.Information,
    421.                     2000
    422.                 )
    423.             self.first_show = False
    424.             event.ignore()
    425.         else:
    426.             # 真正退出程序
    427.             self.tray_icon.hide()
    428.             event.accept()
    429.             
    430.     def confirm_exit(self):
    431.         reply = QMessageBox.question(self, '确认', '是否退出?',
    432.                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
    433.         if reply == QMessageBox.Yes:
    434.             self.tray_icon.hide()
    435.             QApplication.quit()

    436. def main():
    437.     # 防止重复运行
    438.     shared = QSharedMemory("定时关机")
    439.     if not shared.create(512, QSharedMemory.ReadWrite):
    440.         QMessageBox.warning(None, "警告", "程序已经在运行!")
    441.         sys.exit(0)
    442.         
    443.     # 设置高DPI支持
    444.     QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    445.    
    446.     app = QApplication(sys.argv)
    447.     window = ShutdownApp()
    448.    
    449.     # 如果配置了随系统启动且循环执行,则不显示窗口
    450.     config_file = os.path.join(os.getenv('APPDATA'), 'shutdown_config.ini')
    451.     if os.path.exists(config_file):
    452.         config = configparser.ConfigParser()
    453.         config.read(config_file)
    454.         
    455.         if 'task' in config and config['task'].get('auto_start', '0') == '1':
    456.             if config['task'].get('task_circ', '0') == '1':
    457.                 window.showMinimized()
    458.             else:
    459.                 window.show()
    460.         else:
    461.             window.show()
    462.     else:
    463.         window.show()
    464.         
    465.     sys.exit(app.exec_())

    466. if __name__ == '__main__':
    467.     main()
    复制代码
    总结与优化方向


    优点

    界面简洁,操作方便
    系统托盘支持,后台静默运行
    支持定时 & 倒计时模式,满足不同需求

    可优化方向

    支持多任务管理(同时设置多个定时任务)
    增加日志记录(记录每次关机任务)
    增加任务进度条(倒计时可视化显示)
    如果你对这个工具感兴趣,可以尝试优化它,让它变得更加智能!
    以上就是基于PyQt5实现的Windows定时关机工具的详细内容,更多关于PyQt5 Windows定时关机的资料请关注脚本之家其它相关文章!

    来源:https://www.jb51.net/python/339581h36.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

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

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

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