import
sys
from
PyQt5.QtWidgets
import
QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QTimeEdit, QMessageBox
from
PyQt5.QtCore
import
QTime, QTimer
import
os
import
subprocess
class
SystemControlTool(QMainWindow):
def
__init__(
self
):
super
().__init__()
self
.setWindowTitle(
"系统控制工具"
)
self
.setGeometry(
100
,
100
,
300
,
400
)
self
.setStyleSheet(
)
self
.initUI()
def
initUI(
self
):
central_widget
=
QWidget()
self
.setCentralWidget(central_widget)
layout
=
QVBoxLayout()
shutdown_btn
=
QPushButton(
"关机"
)
shutdown_btn.clicked.connect(
self
.shutdown)
layout.addWidget(shutdown_btn)
restart_btn
=
QPushButton(
"重启"
)
restart_btn.clicked.connect(
self
.restart)
layout.addWidget(restart_btn)
sleep_btn
=
QPushButton(
"休眠"
)
sleep_btn.clicked.connect(
self
.sleep)
layout.addWidget(sleep_btn)
layout.addWidget(QLabel(
"定时关机:"
))
self
.time_edit
=
QTimeEdit()
self
.time_edit.setDisplayFormat(
"HH:mm"
)
self
.time_edit.setTime(QTime.currentTime())
layout.addWidget(
self
.time_edit)
schedule_btn
=
QPushButton(
"设置定时关机"
)
schedule_btn.clicked.connect(
self
.schedule_shutdown)
layout.addWidget(schedule_btn)
central_widget.setLayout(layout)
def
shutdown(
self
):
reply
=
QMessageBox.question(
self
,
'确认'
,
'确定要关机吗?'
,
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if
reply
=
=
QMessageBox.Yes:
os.system(
"shutdown /s /t 1"
)
def
restart(
self
):
reply
=
QMessageBox.question(
self
,
'确认'
,
'确定要重启吗?'
,
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if
reply
=
=
QMessageBox.Yes:
os.system(
"shutdown /r /t 1"
)
def
sleep(
self
):
reply
=
QMessageBox.question(
self
,
'确认'
,
'确定要休眠吗?'
,
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if
reply
=
=
QMessageBox.Yes:
os.system(
"rundll32.exe powrprof.dll,SetSuspendState 0,1,0"
)
def
schedule_shutdown(
self
):
current_time
=
QTime.currentTime()
shutdown_time
=
self
.time_edit.time()
if
shutdown_time <
=
current_time:
QMessageBox.warning(
self
,
'错误'
,
'请设置一个未来的时间'
)
return
seconds
=
current_time.secsTo(shutdown_time)
reply
=
QMessageBox.question(
self
,
'确认'
,
f
'将在{shutdown_time.toString("HH:mm")}关机,确认吗?'
,
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if
reply
=
=
QMessageBox.Yes:
os.system(f
"shutdown /s /t {seconds}"
)
QMessageBox.information(
self
,
'成功'
, f
'已设置定时关机: {shutdown_time.toString("HH:mm")}'
)
if
__name__
=
=
"__main__"
:
app
=
QApplication(sys.argv)
window
=
SystemControlTool()
window.show()
sys.exit(app.exec_())