-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain_qt.py
More file actions
291 lines (242 loc) · 8.83 KB
/
main_qt.py
File metadata and controls
291 lines (242 loc) · 8.83 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import sys
import base64
import random,time
import os
from io import BytesIO
import pandas as pd
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon, QFont
name_list = []
called_name_list = []
text_status = 0
def load_names_from_file(file_path):
try:
df = pd.read_excel(file_path, usecols=[0], names=None)
return [i[0] for i in df.values.tolist()]
except Exception as e:
QMessageBox.critical(None, "错误", f"无法读取文件:{e}")
return []
def load_default_file():
default_file = "list.xlsx"
if os.path.exists(default_file):
global name_list
name_list = load_names_from_file(default_file)
if name_list:
return "默认名单已加载"
else:
return "默认名单为空"
else:
return "未找到默认名单,请手动导入"
def file_dialog():
from PyQt5.QtWidgets import QFileDialog
file_path, _ = QFileDialog.getOpenFileName(None, "导入名单", "", "Excel 文件 (*.xlsx);;All Files (*)")
if file_path:
global name_list
name_list = load_names_from_file(file_path)
if name_list:
return "名单已加载"
else:
return "名单为空"
else:
return "未选择文件"
class RandomCall(QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.count=0
self.rmname=""
def initUI(self):
self.setWindowTitle("")
self.setGeometry(300, 300, 400, 280)
self.setFixedSize(self.size())
self.setStyleSheet(
'''
QWidget {
background-color: white;
}
QLabel {
font-family: "黑体";
font-size: 40px;
font-weight: bold;
color: black;
qproperty-alignment: "AlignCenter";
}
QCheckBox {
font-family: "Microsoft YaHei UI";
font-size: 16px;
}
QPushButton {
background-color: #A5D6A7;
height: 40px;
width: 100px;
border-radius: 16px;
font-family: "Microsoft YaHei UI";
font-size: 20px;
color: white;
}
QPushButton:hover {
background-color: #81c784;
}
QPushButton:pressed {
background-color: #66bb6a;
}
QMenuBar {
background-color: white;
}
QMenu {
background-color: white;
}
QMenuBar::item {
spacing: 3px;
padding: 2px 10px;
background-color: transparent;
}
QMenuBar::item:selected {
background-color: #e0e0e0;
}
QMenu::item {
padding: 2px 20px;
}
QMenu::item:selected {
background-color: #e0e0e0;
}
'''
)
# 设置窗口图标
#self.setWindowIcon(QIcon("icon.ico"))
# 初始化状态
global text_status
text_status = 0
self.status_label = QLabel("准备就绪", self)
self.status_label.setFont(QFont("黑体", 40, QFont.Bold))
self.status_label.setAlignment(Qt.AlignCenter)
# 复选框
self.allow_repeat_checkbox = QCheckBox("允许重复点名", self)
self.allow_repeat_checkbox.setChecked(False)
# 按钮
self.call_button = QPushButton("点名", self)
self.call_button.setFont(QFont("黑体", 20))
self.call_button.clicked.connect(self.start)
self.reset_button = QPushButton("重置", self)
self.reset_button.setFont(QFont("黑体", 20))
#self.reset_button.setStyleSheet("background-color: #A5D6A7")
self.reset_button.clicked.connect(self.reset)
self.timer=QTimer(self)
self.timer.timeout.connect(self.update_show)
# 布局
layout = QVBoxLayout()
layout.addWidget(self.status_label)
layout.addWidget(self.allow_repeat_checkbox)
button_layout = QHBoxLayout()
button_layout.addWidget(self.call_button)
button_layout.addWidget(self.reset_button)
layout.addLayout(button_layout)
self.setLayout(layout)
# 菜单栏
self.main_menu = QMenuBar(self)
self.file_menu = self.main_menu.addMenu("文件")
self.help_menu = self.main_menu.addMenu("帮助")
self.show_called_action = QAction("已点名名单", self)
self.show_called_action.triggered.connect(self.called_name_list_show)
self.file_menu.addAction(self.show_called_action)
self.import_action = QAction("导入", self)
self.import_action.triggered.connect(self.import_names)
self.file_menu.addAction(self.import_action)
self.exit_action = QAction("退出", self)
self.exit_action.triggered.connect(self.close)
self.file_menu.addAction(self.exit_action)
self.help_action = QAction("帮助", self)
self.help_action.triggered.connect(self.show_help)
self.help_menu.addAction(self.help_action)
self.about_action = QAction("关于", self)
self.about_action.triggered.connect(self.show_about)
self.help_menu.addAction(self.about_action)
self.init_status()
def init_status(self):
status_text = load_default_file()
if status_text:
self.status_label.setText(status_text)
def update_show(self):
self.count+=1
if not name_list:
if called_name_list:
self.status_label.setText("名单已用完")
else:
self.status_label.setText("名单为空")
self.timer.stop()
self.call_button.setEnabled(True)
self.call_button.setText("点名")
return
else:
name=random.choice(name_list)
self.status_label.setText(name)
if self.count>=10:
self.rmname=name
self.timer.stop()
self.call_button.setEnabled(True)
self.call_button.setText("点名")
if not self.allow_repeat_checkbox.isChecked() and self.rmname in name_list:
called_name_list.append(self.rmname)
if not name_list:
self.status_label.setText("名单已用完")
else:
name_list.remove(self.rmname)
#QApplication.processEvents()
def start(self):
if self.timer.isActive(): #new: 防止重复点名
return
self.count=0
self.call_button.setEnabled(False)
self.call_button.setText("点名中")
self.timer.start(50) #可自定义间隔时间
def reset(self):
global name_list, called_name_list
called_name_list.clear()
self.count=0
load_default_file()
if name_list:
self.status_label.setText("准备就绪")
else:
self.status_label.setText("名单为空")
def called_name_list_show(self):
if not self.allow_repeat_checkbox.isChecked():
if called_name_list:
QMessageBox.information(None, "已点名名单", "\n".join(called_name_list))
else:
QMessageBox.information(None, "已点名名单", "没有已点名名单")
else:
QMessageBox.information(None, "已点名名单", "允许重复点名时,已点名名单不显示")
def import_names(self):
status_text = file_dialog()
if status_text:
self.status_label.setText(status_text)
def show_help(self):
QMessageBox.information(None, "使用说明", "使用说明:\n"
"1. 程序会尝试加载默认文件 list.xlsx\n"
"2. 如果未找到默认文件,请点击“文件 -> 导入”选择名单文件\n"
"3. 可以选择是否允许重复点名\n"
"4. 点击“点名”按钮进行随机点名\n"
"5. 点击“重置”按钮清空已点名名单\n"
"6. 点击“文件 -> 已点名名单”查看已点名名单\n")
def show_about(self):
QMessageBox.information(None, "关于", "随机点名器\n"
"版本 1.2\n"
"原创:Astral & Colipot\n"
"Github 开源地址: github.com/Meltide/RandomCall\n"
"Forked改编仓库:github.com/EricDing618/RandomCall")
if __name__ == '__main__':
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = QMainWindow()
win.setWindowTitle("RandomCall")
win.setGeometry(300, 300, 400, 280)
win.setFixedSize(win.size())
#win.setWindowFlag(Qt.WindowType.WindowMaximizeButtonHint,False)
win.setStyleSheet("background-color: white")
# 设置窗口图标
win.setWindowIcon(QIcon("icon.ico"))
random_call_widget = RandomCall()
win.setCentralWidget(random_call_widget)
win.show()
sys.exit(app.exec())