起因是这样的,因为工作,需要一个定时关机程序,起初呢,我是想到了“任务计划程序”。可惜啊,需要定时关机的时间并不固定,然后我想起来就用命令,如“shutdown -s -t”这样的cmd命令进行处理,但是又遇到一下两个问题 1、每次得计算具体关机的还有多少秒,不是自己转换就是还得找个在线转换网站进行时间转换成秒。 2、其实自动关机的程序是媳妇主要使用,但是她又是个小白,而我又不想(懒得去)每次被叫去操作一番。 于是,就决定写个桌面小程序,点点点就解决问题了。至于为啥用python啥的,因为最近在学习。(小白勿喷,有好的建议可以提出)

# @Author  : D
from PySide6 import QtWidgets, QtCore
from PySide6.QtWidgets import QWidget, QVBoxLayout, QMessageBox
from PySide6.QtCore import QDate, QTime
from PySide6.QtUiTools import QUiLoader
import sys
import os
import time
from datetime import datetime
import subprocess


class Win(QWidget):

    def __init__(self):
        # 从文件中加载UI定义
        super(Win, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.win_ui = QUiLoader().load('autoshutdown.ui')
        # 该函数在Python的Qt库中创建了一个垂直布局(QVBoxLayout),并将此布局应用到self(通常是一个GUI组件如QWidget)上,目的是使self内的控件按照垂直方向进行排列和管理。
        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.win_ui)  # 添加UI
        self.setLayout(self.layout)  # 设置布局

        self.showtime_1()  # 时间设置栏的显示

        timer = QtCore.QTimer(self)  # 定义一个定时器
        # 槽函数showtime_slot/showtime_slot2
        timer.timeout.connect(self.showtime_slot2)
        timer.start(1000)  # 启动定时器,每秒触发一次槽函数

        self.win_ui.btn_shutdown.clicked.connect(
            self.shutdown_con)  # 点击后进行计算后关机
        self.win_ui.cancel_btn.clicked.connect(self.cancel_rule)  # 点击后取消

    def showtime_slot2(self):
        self.str_time = time.strftime(
            "%Y-%m-%d %H:%M:%S", time.localtime())  # 获取当前时间 %X 本地相应的时间表示
        self.win_ui.lcdNumber.display(self.str_time)

    def showtime_1(self):  # 时间模块的显示

        # 时间控件设置为当前日期
        self.win_ui.datatimetext.setDate(QDate.currentDate())
        # 时间控件设置为当前时间
        self.win_ui.datatimetext.setTime(QTime(23, 30))

    def shutdown_con(self):
        dt = str(self.win_ui.datatimetext.dateTime().toString())  # 获取设定的时间

        # 解析原始日期时间字符串
        date_object = datetime.strptime(dt, '%a %b %d %H:%M:%S %Y')

        dt_obj1 = datetime.strptime(
            self.str_time, '%Y-%m-%d %H:%M:%S')  # 将当前时间设置成datetime类型
        dt_obj2 = date_object.strftime('%Y-%m-%d %H:%M:%S')
        dt_obj2 = datetime.strptime(dt_obj2, '%Y-%m-%d %H:%M:%S')
        # 设定时间减去当前日期
        delta = (dt_obj2 - dt_obj1).total_seconds()
        # 转换成数值函数
        delta = int(delta)
        # 利用cmd命令
        os.system(f'shutdown -s -t {delta}')

    def cancel_rule(self):  # 取消关机计划任务

        try:
            subprocess.run(["shutdown", "-a"], check=True)  # 假设"-a"是用于取消关机的参数
        except subprocess.CalledProcessError as e:

            if "因为没有任何进行中的关机过程,所以无法中止系统关机" in str(e.output):
                QMessageBox.about(self, '提示', "错误:没有进行中的关机过程,取消操作失败。")

            else:
                QMessageBox.about(self, '提示', f'执行命令时发生错误:{e}')


app = QtWidgets.QApplication([])  # 创建一个应用程序对象
window = Win()  # 创建一个窗口对象
window.show()  # 显示窗口
sys.exit(app.exec())  # 运行程序

里面用到的ui文件

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>688</width>
    <height>435</height>
   </rect>
  </property>
  <property name="sizePolicy">
   <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
    <horstretch>0</horstretch>
    <verstretch>0</verstretch>
   </sizepolicy>
  </property>
  <property name="minimumSize">
   <size>
    <width>688</width>
    <height>435</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>688</width>
    <height>435</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>自动关机测试工具</string>
  </property>
  <property name="styleSheet">
   <string notr="true">QWidget{
	background-color:rgba(93, 170, 165,20)
}
QPushButton:hover{
	border:3px solid #1d649c;
	color: rgb(255, 255, 255);
	background-color: rgba(85, 170, 127, 150);
}

QPushButton{
	border:3px solid rgba(0, 138, 0, 242) ;
	color: rgb(0, 186, 136);
}

QLabel{color: rgb(0, 186, 136);}


</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>61</width>
     <height>61</height>
    </rect>
   </property>
   <property name="text">
    <string/>
   </property>
   <property name="pixmap">
    <pixmap>../自动关机-测试/images/fbe59d50f55caf5f85f55d447856d29b.jpeg</pixmap>
   </property>
   <property name="scaledContents">
    <bool>true</bool>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>20</y>
     <width>181</width>
     <height>41</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>幼圆</family>
     <pointsize>22</pointsize>
    </font>
   </property>
   <property name="text">
    <string>定时关机工具</string>
   </property>
  </widget>
  <widget class="QDateTimeEdit" name="datatimetext">
   <property name="geometry">
    <rect>
     <x>140</x>
     <y>190</y>
     <width>481</width>
     <height>81</height>
    </rect>
   </property>
   <property name="sizePolicy">
    <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
     <horstretch>0</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
   <property name="font">
    <font>
     <family>方正粗黑宋简体</family>
     <pointsize>18</pointsize>
    </font>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_shutdown">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>300</y>
     <width>161</width>
     <height>71</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>幼圆</family>
     <pointsize>14</pointsize>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>定时关机</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_3">
   <property name="geometry">
    <rect>
     <x>130</x>
     <y>210</y>
     <width>72</width>
     <height>15</height>
    </rect>
   </property>
   <property name="text">
    <string/>
   </property>
  </widget>
  <widget class="QPushButton" name="cancel_btn">
   <property name="geometry">
    <rect>
     <x>470</x>
     <y>300</y>
     <width>161</width>
     <height>71</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>幼圆</family>
     <pointsize>14</pointsize>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>取消定时</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_4">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>170</y>
     <width>121</width>
     <height>111</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>幼圆</family>
     <pointsize>18</pointsize>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>时间选择:</string>
   </property>
   <property name="textFormat">
    <enum>Qt::AutoText</enum>
   </property>
   <property name="scaledContents">
    <bool>false</bool>
   </property>
   <property name="wordWrap">
    <bool>false</bool>
   </property>
  </widget>
  <widget class="QLCDNumber" name="lcdNumber">
   <property name="geometry">
    <rect>
     <x>140</x>
     <y>80</y>
     <width>481</width>
     <height>91</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true">color: rgb(255, 170, 0);</string>
   </property>
   <property name="digitCount">
    <number>19</number>
   </property>
   <property name="mode">
    <enum>QLCDNumber::Dec</enum>
   </property>
   <property name="segmentStyle">
    <enum>QLCDNumber::Flat</enum>
   </property>
   <property name="intValue" stdset="0">
    <number>0</number>
   </property>
  </widget>
  <widget class="QLabel" name="label_5">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>80</y>
     <width>121</width>
     <height>91</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>幼圆</family>
     <pointsize>18</pointsize>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>当前时间:</string>
   </property>
   <property name="textFormat">
    <enum>Qt::AutoText</enum>
   </property>
   <property name="scaledContents">
    <bool>false</bool>
   </property>
   <property name="wordWrap">
    <bool>false</bool>
   </property>
  </widget>
 </widget>
 <resources>
  <include location="img.qrc"/>
 </resources>
 <connections/>
</ui>

完成后就是这样的 ,以后只需要点点点,默认设置的为23:30 因为要关机了,省着在手改时间,当然 你喜欢几点就更改为几点,反正有了这个,没再被喊去调整关机。 1.png