-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimed-Shutdown.py
More file actions
227 lines (202 loc) · 8.35 KB
/
Timed-Shutdown.py
File metadata and controls
227 lines (202 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import sys
import os
import time
import threading
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QSpinBox, QPushButton,
QVBoxLayout, QHBoxLayout, QGroupBox, QRadioButton,
QDateTimeEdit, QProgressBar, QMessageBox)
from PyQt5.QtCore import QDateTime, QTimer, Qt
class ShutdownTimerApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("智能定时关机")
self.setGeometry(300, 300, 400, 250)
self.setWindowFlags(self.windowFlags() | Qt.WindowMinimizeButtonHint) # 添加最小化按钮
self.shutdown_timer = None
self.remaining_time = 0
self.initUI()
self.applyStylesheet()
def initUI(self):
main_layout = QVBoxLayout()
# 关机模式选择
mode_group = QGroupBox("选择关机模式")
mode_layout = QHBoxLayout()
self.radio_specific_time = QRadioButton("指定时间")
self.radio_delay = QRadioButton("延迟时间")
self.radio_delay.setChecked(True) # 默认选择延迟时间
mode_layout.addWidget(self.radio_specific_time)
mode_layout.addWidget(self.radio_delay)
mode_group.setLayout(mode_layout)
main_layout.addWidget(mode_group)
# 指定时间设置
self.specific_time_group = QGroupBox("指定关机时间")
self.specific_time_layout = QHBoxLayout()
self.datetime_edit = QDateTimeEdit(QDateTime.currentDateTime().addSecs(3600)) # 默认一小时后
self.specific_time_layout.addWidget(self.datetime_edit)
self.specific_time_group.setLayout(self.specific_time_layout)
main_layout.addWidget(self.specific_time_group)
self.specific_time_group.setEnabled(False) # 默认禁用
# 延迟时间设置
self.delay_group = QGroupBox("设置延迟时间")
self.delay_layout = QHBoxLayout()
self.hours_spin = QSpinBox()
self.hours_spin.setRange(0, 23)
self.minutes_spin = QSpinBox()
self.minutes_spin.setRange(0, 59)
self.delay_layout.addWidget(QLabel("小时:"))
self.delay_layout.addWidget(self.hours_spin)
self.delay_layout.addWidget(QLabel("分钟:"))
self.delay_layout.addWidget(self.minutes_spin)
self.delay_group.setLayout(self.delay_layout)
main_layout.addWidget(self.delay_group)
# 控制按钮
control_layout = QHBoxLayout()
self.start_button = QPushButton("启动定时")
self.cancel_button = QPushButton("取消定时")
self.cancel_button.setEnabled(False) # 初始禁用取消按钮
control_layout.addWidget(self.start_button)
control_layout.addWidget(self.cancel_button)
main_layout.addLayout(control_layout)
# 倒计时显示
self.countdown_label = QLabel("尚未启动定时")
self.countdown_label.setAlignment(Qt.AlignCenter)
main_layout.addWidget(self.countdown_label)
# 进度条
self.progress_bar = QProgressBar()
self.progress_bar.setValue(0)
main_layout.addWidget(self.progress_bar)
self.setLayout(main_layout)
# 连接信号和槽
self.radio_specific_time.toggled.connect(self.toggleSpecificTime)
self.radio_delay.toggled.connect(self.toggleDelay)
self.start_button.clicked.connect(self.startShutdown)
self.cancel_button.clicked.connect(self.cancelShutdown)
self.timer = QTimer(self)
self.timer.timeout.connect(self.updateCountdown)
def applyStylesheet(self):
self.setStyleSheet("""
QWidget {
background-color: #f0f0f0;
font-family: "Segoe UI", "Microsoft YaHei", sans-serif;
font-size: 10pt;
}
QGroupBox {
border: 1px solid #ccc;
border-radius: 5px;
margin-top: 10px;
}
QGroupBox::title {
subcontrol-origin: margin;
left: 10px;
padding: 0 5px;
color: #555;
}
QLabel {
color: #333;
}
QPushButton {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
border-radius: 5px;
font-size: 10pt;
}
QPushButton:hover {
background-color: #45a049;
}
QPushButton:pressed {
background-color: #367c39;
}
QPushButton:disabled {
background-color: #ccc;
color: #888;
}
QSpinBox, QDateTimeEdit {
border: 1px solid #ccc;
border-radius: 3px;
padding: 5px;
background-color: white;
}
QProgressBar {
border: 1px solid #ccc;
border-radius: 5px;
text-align: center;
}
QProgressBar::chunk {
background-color: #5cb85c;
border-radius: 5px;
}
""")
def toggleSpecificTime(self, checked):
self.specific_time_group.setEnabled(checked)
self.delay_group.setEnabled(not checked)
def toggleDelay(self, checked):
self.delay_group.setEnabled(checked)
self.specific_time_group.setEnabled(not checked)
def startShutdown(self):
if self.radio_delay.isChecked():
hours = self.hours_spin.value()
minutes = self.minutes_spin.value()
total_seconds = (hours * 3600) + (minutes * 60)
if total_seconds > 0:
self.startTimer(total_seconds)
else:
QMessageBox.warning(self, "警告", "请设置大于零的延迟时间。")
elif self.radio_specific_time.isChecked():
target_datetime = self.datetime_edit.dateTime()
current_datetime = QDateTime.currentDateTime()
if target_datetime > current_datetime:
diff_seconds = current_datetime.secsTo(target_datetime)
self.startTimer(diff_seconds)
else:
QMessageBox.warning(self, "警告", "请设置未来的关机时间。")
def startTimer(self, total_seconds):
self.remaining_time = total_seconds
self.progress_bar.setMaximum(total_seconds)
self.progress_bar.setValue(total_seconds)
self.updateCountdownDisplay()
self.start_button.setEnabled(False)
self.cancel_button.setEnabled(True)
self.radio_specific_time.setEnabled(False)
self.radio_delay.setEnabled(False)
self.timer.start(1000) # 每秒更新一次
# 启动关机线程
self.shutdown_timer = threading.Timer(total_seconds, self.shutdownSystem)
self.shutdown_timer.start()
def cancelShutdown(self):
if self.shutdown_timer and self.shutdown_timer.is_alive():
self.shutdown_timer.cancel()
self.shutdown_timer = None
self.timer.stop()
self.countdown_label.setText("定时已取消")
self.progress_bar.setValue(0)
self.start_button.setEnabled(True)
self.cancel_button.setEnabled(False)
self.radio_specific_time.setEnabled(True)
self.radio_delay.setEnabled(True)
def updateCountdown(self):
self.remaining_time -= 1
self.updateCountdownDisplay()
self.progress_bar.setValue(self.remaining_time)
if self.remaining_time <= 0:
self.timer.stop()
self.countdown_label.setText("正在关机...")
def updateCountdownDisplay(self):
hours = self.remaining_time // 3600
minutes = (self.remaining_time % 3600) // 60
seconds = self.remaining_time % 60
self.countdown_label.setText(f"剩余时间: {hours:02d}:{minutes:02d}:{seconds:02d}")
def shutdownSystem(self):
os_name = os.name
if os_name == 'nt': # Windows
os.system("shutdown /s /t 0")
elif os_name == 'posix': # Linux, macOS
os.system("shutdown -h now")
else:
QMessageBox.warning(self, "警告", "不支持的操作系统,无法执行关机。")
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ShutdownTimerApp()
window.show()
sys.exit(app.exec_())