-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
206 lines (178 loc) · 6.86 KB
/
main.py
File metadata and controls
206 lines (178 loc) · 6.86 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
from random import random
from PyQt5.QtCore import Qt, QSize
from qtconsole.qt import QtCore
from qtpy import QtWidgets
from PyQt5.QtWidgets import *
import sys
import win32clipboard as w
import win32con
import utils
from User import User
from ui.mainWindow import Ui_MainWindow
from ui.newUserDialog import Ui_Dialog
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
# 添加用户 按钮
button = self.add_user_button
button.clicked.connect(self.showDialog)
# 已选择的用户
self.checkUserList = []
# 已有用户列表
self.initUserTable()
# 选择列表click
self.toChooseListInit()
def showDialog(self):
self.addUserDialog = AddUserDialog(self)
self.addUserDialog.setWindowTitle('对话框')
self.addUserDialog.setWindowModality(Qt.ApplicationModal)
self.addUserDialog.show()
def initUserTable(self):
# 设置表格属性
table = self.tableWidget
table.setFrameShape(QFrame.NoFrame)
table.setEditTriggers(QAbstractItemView.NoEditTriggers) # 设置表格不可更改
table.setSelectionMode(QAbstractItemView.NoSelection)
# 加载用户
self.loadUser()
def tableAddLine(self, newUser):
user = User(newUser["studentId"], newUser["name"], newUser["classId"])
table = self.tableWidget
row = table.rowCount()
table.setRowCount(row + 1)
studentId = user.studentId
name = user.name
classId = user.classId
##下面六行用于生成居中的checkbox,不知道有没有别的好方法
ck = QCheckBox()
h = QHBoxLayout()
h.setAlignment(Qt.AlignCenter)
h.addWidget(ck)
w = QWidget()
w.setLayout(h)
table.setItem(row, 0, QTableWidgetItem(name))
table.setItem(row, 1, QTableWidgetItem(studentId))
table.setItem(row, 2, QTableWidgetItem(classId))
table.setCellWidget(row, 3, w)
self.checkUserList.append([name, studentId, classId, ck])
def loadUser(self):
users = utils.loadUsers()
# 清空准备重新更新
self.checkUserList = []
self.tableWidget.setRowCount(0)
for user in users:
self.tableAddLine(user)
# click绑定可选择按钮
# TODO 自定义,弹出Dialog完成自定义输入
def toChooseListInit(self):
self.choose_form.setStyleSheet("QListWidget{border:0px solid gray; color:black; }")
studentIdButton = self.choose_studentId
classIdButton = self.choose_classId
nameButton = self.choose_name
customButton = self.choose_custom
clearButton = self.clear_choose_button
removeButton = self.remove_user_button
studentIdButton.clicked.connect(self.clickChooseStudent)
classIdButton.clicked.connect(self.clickChooseClassId)
nameButton.clicked.connect(self.clickChooseName)
customButton.clicked.connect(self.clickChooseCustom)
clearButton.clicked.connect(self.clickClear)
removeButton.clicked.connect(self.clickRemoveUser)
self.generate_result_button.clicked.connect(self.generateResult)
self.generate_clipbord_button.clicked.connect(self.clickClipbord)
def clickChooseStudent(self):
wightItem = QListWidgetItem()
wightItem.setText("学号")
wightItem.setTextAlignment(Qt.AlignCenter)
wightItem.setSizeHint(QSize(100, 32))
self.choose_form.addItem(wightItem)
def clickChooseClassId(self):
self.choose_form.addItem("班级")
def clickChooseName(self):
self.choose_form.addItem("姓名")
def clickChooseCustom(self):
text, okPressed = QInputDialog.getText(self, "自定义", "Input:", QLineEdit.Normal, "")
if okPressed and text != '':
self.choose_form.addItem(text)
def clickClear(self):
chooseList = self.choose_form
if chooseList.currentRow() == -1:
chooseList.clear()
else:
chooseList.takeItem(chooseList.currentRow())
def clickRemoveUser(self):
saveUserList = []
for userItem in self.checkUserList:
if not userItem[3].isChecked():
saveUserList.append(User(userItem[1], userItem[0], userItem[2]))
utils.saveUserList(saveUserList)
self.loadUser()
def clickClipbord(self):
w.OpenClipboard()
w.EmptyClipboard()
# TODO 弹框提示 复制成功
w.SetClipboardData(win32con.CF_UNICODETEXT, self.textBrowser.toPlainText())
w.CloseClipboard()
def generateResult(self):
global result
choosedKindList = []
for i in range(self.choose_form.count()):
choosedKindList.append(self.choose_form.item(i).text())
for useritem in self.checkUserList:
if useritem[3].isChecked():
self.textBrowser.setHtml("")
break
for userItem in self.checkUserList:
if userItem[3].isChecked():
userInfoArray = []
for choosedInfo in choosedKindList:
if choosedInfo == "姓名":
userInfoArray.append(userItem[0])
elif choosedInfo == "学号":
userInfoArray.append(userItem[1])
elif choosedInfo == "班级":
userInfoArray.append(userItem[2])
else:
userInfoArray.append(choosedInfo)
result = self.choose_delimiter.currentText().join(userInfoArray)
self.textBrowser.append(result)
# def clickChooseCustom(self):
# # self.chooseList.append("studentId")
# # self.choosed_label.text() + " "
class AddUserDialog(QDialog, Ui_Dialog):
def __init__(self, mainWindow, parent=None, ):
self.mainWindow = mainWindow
super(AddUserDialog, self).__init__(parent)
self.setupUi(self)
add = self.add_user
cancel = self.cancel
add.clicked.connect(self.addUser)
cancel.clicked.connect(self.close)
def addUser(self):
user = User(self.student_id.text(), self.name.text(), self.class_id.text())
utils.saveUser(user)
self.mainWindow.loadUser()
self.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.resize(960, 540)
win.setFixedSize(960, 540)
win.show()
sys.exit(app.exec_())
# studentId = input("学号:")
# name = input("姓名:")
# classId = input("班号:")
#
# user = User(studentId, name, classId)
#
# delimiter = input("分隔符:")
#
# array = []
#
# array.append(user.studentId)
# array.append(user.name)
#
# result = delimiter.join(array)
# print(result)