刚开始为了熟系框架写的,实际上cmd窗口更省事,打包也更省空间。
# -*- coding: utf-8 -*-
# @Time : 2023/9/10
# @Author : _Setsumi
# @Site :
# @File : windows定时关机.py
# @Software: Vscode
import sys, os, datetime, re
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QTextEdit, QPushButton, QVBoxLayout, QHBoxLayout
class Main(QWidget):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.setWindowTitle("设置定时关机")
self.setFixedSize(300, 300)
self.sd = ShutDown()
self.initUI()
def initUI(self):
mainLayout = QVBoxLayout()
widget1 = QWidget()
layout1 = QHBoxLayout()
label1 = QLabel("设置时间\n(空格分割):")
led1 = QLineEdit()
layout1.addWidget(label1)
layout1.addWidget(led1)
widget1.setLayout(layout1)
layout2 = QHBoxLayout()
widget2 = QWidget()
btnSeShutDown = QPushButton("设置")
btnSeShutDown.clicked.connect(lambda :tedPreview.setText(self.sd.setDown(led1.text())))
btnCancel = QPushButton("取消")
btnCancel.clicked.connect(lambda :tedPreview.setText(self.sd.cancel()))
btnCancel.clicked.connect(self.sd.cancel)
layout2.addWidget(btnSeShutDown)
layout2.addWidget(btnCancel)
widget2.setLayout(layout2)
tedPreview = QTextEdit()
tedPreview.setReadOnly(True)
for widget in [widget1, widget2, tedPreview]:
mainLayout.addWidget(widget)
self.setLayout(mainLayout)
class ShutDown:
def __init__(self):
pass
def setDown(self, inputTime):
self.cancel()
check = re.search("^[0-9]+ [0-9]+$", inputTime)
inputTime = inputTime.split(" ")
if check:
inputHour, inputMin = int(inputTime[0]), int(inputTime[1])
if 0<=inputHour<=24 and 0<=inputMin<=60:
currentTime = datetime.datetime.now()
currentHour = currentTime.hour
currentMin = currentTime.minute
hours = (inputHour + (inputMin / 60)) - (currentHour + (currentMin / 60))
mins = hours * 60
seconds = mins * 60
os.system("shutdown -s -t %d"%seconds)
return "设置成功,距离关机还有%d分钟" % mins
else:
return "时间范围错误"
else:
return "格式错误"
def cancel(self):
os.system("shutdown -a")
return "取消成功"
if __name__ == "__main__":
app = QApplication(sys.argv)
run = Main()
run.show()
sys.exit(app.exec_())