吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1688|回复: 59
收起左侧

[原创工具] python写的 定时关机(主打一个好看)

  [复制链接]
ygq170063 发表于 2025-12-10 10:59



同类型的软件很多,但是我这个好看呀~~~~



成品:https://fjjy.lanzoub.com/iMtkD3d98v9e


附源码:
[Python] 纯文本查看 复制代码
import sys
import os
from PyQt5.QtWidgets import (QApplication, QWidget, QCheckBox, QLabel, QGroupBox, 
QRadioButton, QDateEdit, QTimeEdit, QPushButton, 
QVBoxLayout, QHBoxLayout, QGridLayout, QStyle, 
QSpacerItem, QSizePolicy, QMessageBox, QStackedWidget,
QSpinBox)
from PyQt5.QtCore import QDate, QTime, QTimer, QDateTime, Qt
from PyQt5.QtGui import QIcon, QFont

class PowerOffWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
    
    def initUI(self):
        self.setWindowTitle("PowerOff")
        self.setWindowIcon(self.style().standardIcon(QStyle.SP_ComputerIcon))
        self.setFixedSize(500, 450)
        self.setFont(QFont("SimHei", 12))

        self.setStyleSheet("""
            QWidget {
                background-color: #ffffff;
                color: #333;
            }

            QGroupBox {
                font-size: 14pt;
                font-weight: bold;
                color: #0d6efd;
                border: 2px solid #0d6efd;
                border-radius: 8px;
                margin-top: 15px;
                padding: 10px 15px;
                background: white;
            }
            QGroupBox::title {
                left: 15px;
                top: -10px;
                background: white;
                padding: 0 10px;
            }

            QLabel#currentTimeLabel {
                background: #0d6efd;
                color: white;
                padding: 8px 15px;
                border-radius: 6px;
                font-size: 13pt;
                min-width: 200px;
                text-align: center;
            }

            QPushButton {
                padding: 8px 20px;
                border-radius: 6px;
                font-size: 13pt;
                color: white;
                background: #0d6efd;
                border: none;
            }
            QPushButton:hover {
                background: #0b5ed7;
            }
            QPushButton#shutdownNowBtn {
                background: #dc3545;
            }
            QPushButton#shutdownNowBtn:hover {
                background: #bb2d3b;
            }
        QPushButton#cancelBtn {
            background: #ffc107;
        }
        QPushButton#cancelBtn:hover {
            background: #e0a800;
        }
            QDateEdit, QTimeEdit, QSpinBox {
                padding: 6px;
                border: 1px solid #ced4da;
                border-radius: 4px;
                width: 300px;
            }
            QCheckBox, QRadioButton {
                spacing: 8px;
                font-size: 12pt;
            }
        """)

        mainLayout = QVBoxLayout(self)
        mainLayout.setContentsMargins(20, 20, 20, 20)
        mainLayout.setSpacing(20)

        topLayout = QHBoxLayout()
        autoRunChk = QCheckBox("开机自启")
        self.timeLabel = QLabel()
        self.timeLabel.setObjectName("currentTimeLabel")
        
        topLayout.addWidget(autoRunChk)
        topLayout.addSpacerItem(QSpacerItem(5, 5, QSizePolicy.Expanding, QSizePolicy.Minimum))
        topLayout.addWidget(self.timeLabel)
        mainLayout.addLayout(topLayout)

        timeGroup = QGroupBox("设置关机时间")
        timeLayout = QVBoxLayout()
        timeLayout.setSpacing(15)

        methodLayout = QHBoxLayout()
        methodLayout.addWidget(QLabel("计时方式:"))
        methodLayout.addStretch()
        self.specRadio = QRadioButton("指定时间")
        self.specRadio.setChecked(True)
        methodLayout.addWidget(self.specRadio)
        methodLayout.addStretch()
        self.countRadio = QRadioButton("递减方式")
        methodLayout.addWidget(self.countRadio)
        timeLayout.addLayout(methodLayout)

        self.stackedWidget = QStackedWidget()
        
        specTimeWidget = QWidget()
        dtGrid = QGridLayout(specTimeWidget)
        dtGrid.setSpacing(10)
        dtGrid.setAlignment(Qt.AlignLeft)
        dtGrid.addWidget(QLabel("关机日期:"), 0, 0, Qt.AlignRight)
        self.dateEdit = QDateEdit(QDate.currentDate())
        self.dateEdit.setDisplayFormat("yyyy年M月d日")
        self.dateEdit.setCalendarPopup(True)
        dtGrid.addWidget(self.dateEdit, 0, 1)
        dtGrid.addWidget(QLabel("关机时间:"), 1, 0, Qt.AlignRight)
        self.timeEdit = QTimeEdit(QTime.currentTime())
        self.timeEdit.setDisplayFormat("h:mm:ss")
        dtGrid.addWidget(self.timeEdit, 1, 1)

        countDownWidget = QWidget()
        countLayout = QHBoxLayout(countDownWidget)
        countLayout.setAlignment(Qt.AlignLeft)
        self.hourSpin = QSpinBox()
        self.hourSpin.setRange(0, 99)
        self.minSpin = QSpinBox()
        self.minSpin.setRange(0, 59)
        self.secSpin = QSpinBox()
        self.secSpin.setRange(0, 59)
        countLayout.addWidget(self.hourSpin)
        countLayout.addWidget(QLabel("小时"))
        countLayout.addSpacing(15)
        countLayout.addWidget(self.minSpin)
        countLayout.addWidget(QLabel("分钟"))
        countLayout.addSpacing(15)
        countLayout.addWidget(self.secSpin)
        countLayout.addWidget(QLabel("秒"))
        
        self.stackedWidget.addWidget(specTimeWidget)
        self.stackedWidget.addWidget(countDownWidget)
        timeLayout.addWidget(self.stackedWidget)

        timeGroup.setLayout(timeLayout)
        mainLayout.addWidget(timeGroup)

        optGroup = QGroupBox("设置关机选项")
        optLayout = QHBoxLayout()
        optLayout.setSpacing(20)
        self.shutRadio = QRadioButton("关机")
        self.shutRadio.setChecked(True)
        self.logRadio = QRadioButton("注销")
        self.rebootRadio = QRadioButton("重启")
        self.forceChk = QCheckBox("强制")
        self.forceChk.setChecked(True)
        optLayout.addStretch()
        optLayout.addWidget(self.shutRadio)
        optLayout.addStretch()
        optLayout.addWidget(self.logRadio)
        optLayout.addStretch()
        optLayout.addWidget(self.rebootRadio)
        optLayout.addStretch()
        optLayout.addWidget(self.forceChk)
        optLayout.addStretch()
        optGroup.setLayout(optLayout)
        mainLayout.addWidget(optGroup)

        btnLayout = QHBoxLayout()
        btnLayout.addStretch()
        self.okBtn = QPushButton("确定")
        self.cancelBtn = QPushButton("取消计划")
        self.cancelBtn.setObjectName("cancelBtn")
        self.nowBtn = QPushButton("立即关机")
        self.nowBtn.setObjectName("shutdownNowBtn")
        btnLayout.addWidget(self.okBtn)
        btnLayout.addSpacing(15)
        btnLayout.addWidget(self.cancelBtn)
        btnLayout.addSpacing(15)
        btnLayout.addWidget(self.nowBtn)
        mainLayout.addLayout(btnLayout)

        self.okBtn.clicked.connect(self.handle_ok)
        self.cancelBtn.clicked.connect(self.cancel_shutdown)
        self.nowBtn.clicked.connect(self.handle_shutdown_now)
        self.specRadio.toggled.connect(self.update_input_mode)

        self.timer = QTimer()
        self.timer.timeout.connect(self.updateTime)
        self.timer.start(1000)
        self.updateTime()
        self.update_input_mode()

    def update_input_mode(self):
        if self.specRadio.isChecked():
            self.stackedWidget.setCurrentIndex(0)
        else:
            self.stackedWidget.setCurrentIndex(1)
            
    def handle_ok(self):
        seconds = 0
        action_name, msg = "", ""

        if self.specRadio.isChecked():
            target_dt = QDateTime(self.dateEdit.date(), self.timeEdit.time())
            seconds = QDateTime.currentDateTime().secsTo(target_dt)
            if seconds <= 0:
                QMessageBox.warning(self, "错误", "指定时间必须晚于当前时间!")
                return
            action_name = self.get_action_name()
            msg = f"已设定计划任务:\n\n将在 {target_dt.toString('yyyy-MM-dd HH:mm:ss')} 执行 {action_name} 操作。"
        else:
            hours = self.hourSpin.value()
            minutes = self.minSpin.value()
            secs = self.secSpin.value()
            seconds = hours * 3600 + minutes * 60 + secs
            if seconds <= 0:
                QMessageBox.warning(self, "错误", "倒计时必须大于0秒!")
                return
            action_name = self.get_action_name()
            msg = f"已设定计划任务:\n\n将在 {hours} 小时 {minutes} 分钟 {secs} 秒后执行 {action_name} 操作。"

        self.execute_shutdown_command(seconds, msg)

    def handle_shutdown_now(self):
        action_name = self.get_action_name()
        reply = QMessageBox.question(self, "确认操作", f"您确定要立即{action_name}吗?",
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            msg = f"系统将立即执行 {action_name} 操作。"
            self.execute_shutdown_command(1, msg)
            
    def cancel_shutdown(self):
        os.system("shutdown -a")
        QMessageBox.information(self, "操作成功", "所有已计划的关机任务已被取消。")

    def execute_shutdown_command(self, seconds, msg):
        if self.shutRadio.isChecked():
            action_flag = "-s"
        elif self.rebootRadio.isChecked():
            action_flag = "-r"
        else:
            action_flag = "-l"
            
        force_flag = "-f" if self.forceChk.isChecked() else ""
        
        command = f"shutdown {action_flag} {force_flag} -t {seconds}"
        os.system(command)
        QMessageBox.information(self, "计划任务已设定", msg)

    def get_action_name(self):
        if self.shutRadio.isChecked():
            return "关机"
        elif self.rebootRadio.isChecked():
            return "重启"
        else:
            return "注销"

    def updateTime(self):
        self.timeLabel.setText(QDateTime.currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyle("Fusion")
    win = PowerOffWidget()
    win.show()
    sys.exit(app.exec_())

免费评分

参与人数 6吾爱币 +11 热心值 +5 收起 理由
lm4469 + 1 谢谢@Thanks!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
weihai0631 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
倾情 + 1 我很赞同!
zoshl + 1 + 1 谢谢@Thanks!
听风唱歌 + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

ypcok 发表于 2025-12-10 11:22
下了。
本来想美一美,结果我的老win7不能运行
 楼主| ygq170063 发表于 2025-12-10 11:51
ysjd22 发表于 2025-12-10 11:46
楼主又来了  上一个html版的视频播放器也挺不错的

还有很多软件 发了后 被论坛吞了,说要审核,无语,我也不知道要审核啥,发不出来,发出来的 都是小儿科的东西,真正给力的 发不出来。。。。。
xfriend360 发表于 2025-12-10 11:03
cndy 发表于 2025-12-10 11:05
请问关机前有弹窗提醒吗
 楼主| ygq170063 发表于 2025-12-10 11:08
cndy 发表于 2025-12-10 11:05
请问关机前有弹窗提醒吗

全自动的。到点就关
jzy288 发表于 2025-12-10 11:08
蛮不错,谢谢提供……
zt185 发表于 2025-12-10 11:09
的确很美观哈哈,感谢楼主下载了!
听风唱歌 发表于 2025-12-10 11:11
感觉很朴、实很适用,EXE文件站内存17.2MB,然后被  那个啥给认定为  Trojan.Generic 关了禁闭。代码收下学习,谢谢。
McKagan 发表于 2025-12-10 11:13
感谢,如果能加个定时开机的功能就完美了
 楼主| ygq170063 发表于 2025-12-10 11:14
听风唱歌 发表于 2025-12-10 11:11
感觉很朴、实很适用,EXE文件站内存17.2MB,然后被  那个啥给认定为  Trojan.Generic 关了禁闭。代码收下学 ...

报毒是不可能报毒的。现在的电脑应该都不缺那点内存吧
策士 发表于 2025-12-10 11:15
如果能加个定时开机的功能就完美了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-12-12 11:42

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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