废话不多说,直接上源码
python
from PySide2.QtWidgets import QApplication, QMessageBox
from PySide2.QtUiTools import QUiLoader
import os
class Stats:
def __init__(self):
self.ui = QUiLoader().load('main.ui')
self.ui.pushButton_2.clicked.connect(self.cancel)
self.ui.pushButton.clicked.connect(self.close)
def close(self):
res = int(self.ui.spinBox.text()) * 60
if res==0:
choice = QMessageBox.question(
self.ui,
'确认',
'确定要立即关机吗?')
if choice == QMessageBox.Yes:
cmd = "shutdown -s -t " + str(res)
os.popen(cmd)
else:
choice = QMessageBox.question(
self.ui,
'确认',
'确定要立即关机吗?')
if choice == QMessageBox.Yes:
cmd = "shutdown -s -t " + str(res)
os.popen(cmd)
def cancel(self):
choice = QMessageBox.question(
self.ui,
'确认',
'确定要取消关机吗?')
if choice == QMessageBox.Yes:
cmd = "shutdown -a"
os.popen(cmd)
app = QApplication([])
stats = Stats()
stats.ui.show()
app.exec_()
main.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>265</width>
<height>152</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>定时关机</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>请输入需要将在多少分钟后系统自动关机</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QSpinBox" name="spinBox"/>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>分钟</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>关机</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>取消关机</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>