-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.py
More file actions
794 lines (607 loc) · 27.8 KB
/
Copy pathaction.py
File metadata and controls
794 lines (607 loc) · 27.8 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
# -*- coding: utf-8 -*-
import sys, os
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QSyntaxHighlighter
from PyQt5.QtWidgets import (
QMainWindow,
QWidget,
QApplication,
QMenu,
QFileDialog,
qApp
)
from ui.Ui_start import Ui_startWindow
from highlight import highlightEditor
from ui import Ui_easy, Ui_hard
root = os.path.split(os.path.abspath(__file__))[0]
class startWindow(QWidget, Ui_startWindow):
"""
初始窗口逻辑\n
用于选择课程和模式
"""
def __init__(self, parent=None):
super(startWindow, self).__init__(parent)
self.setupUi(self)
self.mode = 0
self.course = 0
self.easyButton.clicked.connect(self.updateIntro)
self.hardButton.clicked.connect(self.updateIntro)
self.easyButton.clicked.connect(self.enableYesBtn)
self.hardButton.clicked.connect(self.enableYesBtn)
self.comboBox.currentIndexChanged.connect(self.updateCourse)
self.comboBox.currentIndexChanged.connect(self.enableYesBtn)
self.yesButton.clicked.connect(self.changeWindow)
self.setWindowFlags(Qt.FramelessWindowHint)
self.setWindowOpacity(0.9)
def updateIntro(self):
"""
更新介绍框内容
"""
sender = self.sender()
_translate = QtCore.QCoreApplication.translate
if sender == self.easyButton:
self.introductionBox.setPlainText(
_translate(
"Form",
"选择题模式介绍:\n\n"
"简单模式\n"
"只需要选择给出的代码块\n"
"点击确认进入选择题模式"))
self.mode = 1
elif sender == self.hardButton:
self.introductionBox.setPlainText(
_translate(
"Form",
"填空题模式介绍:\n\n"
"较难模式\n"
"需要自己完成程序中缺失的代码\n"
"点击确认进入填空题模式"))
self.mode = 2
else:
self.yesButton.setEnabled(False)
self.introductionBox.setPlainText(_translate("Form", "模式介绍"))
self.mode = 0
def updateCourse(self, event):
"""
将课程参数同步为下拉菜单内所选内容
"""
self.course = self.comboBox.currentIndex()
# print(self.course)
def changeWindow(self, event):
"""
切换窗口
"""
mode = self.mode
course = self.course
if mode == 1:
self.mainWindow = easyWindow()
elif mode == 2:
self.mainWindow = hardWindow()
self.loadText(course)
self.mainWindow.menu = menu(self.mainWindow)
self.mainWindow.menu.mode = mode
self.mainWindow.back2HomeBtn.clicked.connect(self.show)
self.hide() # close
self.mainWindow.showFirstQuestion()
# print(11111111)
self.mainWindow.show()
def loadText(self, course):
"""
载入代码文本
"""
rsc = root + '/CourseCode'
f1 = open(rsc + '/test'+str(course)+'.py')
f2 = open(rsc + '/answer'+str(course)+'.py')
f3 = open(rsc + '/divide'+str(course)+'.txt')
temp3 = f1.readlines()
temp1 = f2.read()
temp2 = f3.readline()
f1.close()
f2.close()
f3.close()
# print('test file open condition')
# print('test1')
answerSet = []
tmpList = temp1.split('@')
for i in range(int(len(tmpList)/3)):
tmp = tmpList[3*i+2]
tmp = tmp[:-1]
answerSet.append([tmpList[3*i], tmpList[3*i+1], tmp])
# print('test2')
divideSet = []
divideSet = temp2.split('@')
# print('test3')
questionCount = 0
questionSet = []
lineIndex = 0
for i in range(len(temp3)):
if temp3[i].find('___') != -1:
questionCount += 1
for i in range(questionCount):
text = ''
linesNumber = int( divideSet[i] )
for lines in temp3[lineIndex: lineIndex + linesNumber]:
text += lines
lineIndex += linesNumber
questionSet.append(text)
self.mainWindow.questionSet = questionSet
self.mainWindow.questionCount = questionCount
self.mainWindow.answerSet = answerSet
# self.mainWindow.divideSet = divideSet
def enableYesBtn(self, event):
"""
启用/禁用“确认”按钮
"""
if (self.mode != 0) and (self.course != 0):
self.yesButton.setEnabled(True)
self.yesButton.setStyleSheet("background-color: rgb(108, 0, 255);\n color: rgb(255, 255, 255);\nborder-radius: 5px;\npadding: 6px 24px 6px 24px;")
else:
self.yesButton.setEnabled(False)
self.yesButton.setStyleSheet("background-color: rgb(207, 200, 187);\n color: rgb(255, 255, 255);\nborder-radius: 5px;\npadding: 6px 24px 6px 24px;")
class easyWindow(QMainWindow, Ui_easy.Ui_MainWindow):
"""
选择题模式窗口逻辑
"""
questionSet = []
answerSet = []
# divideSet = []
questionCount = 0
selfAnswerSet = []
# lineIndex = 0
# answerText = ''
def __init__(self):
super(easyWindow, self).__init__()
self.setupUi(self)
# 加入文本框
self.highlighter_1 = highlightEditor(self.centralwidget, True)
self.codeBox = self.highlighter_1.editor
# self.codeBox = QtWidgets.QTextEdit(self.centralwidget)
self.horizontalLayout_3.addWidget(self.codeBox)
# connection 按键
# self.backButton.clicked.connect(self.close)
# self.nextButton.clicked.connect(self.nextQuestion)
# self.previousButton.clicked.connect(self.previousQuestion)
# self.radioButton_a.clicked.connect(self.enableNextButton)
# self.radioButton_b.clicked.connect(self.enableNextButton)
# self.radioButton_c.clicked.connect(self.enableNextButton)
# self.radioButton_d.clicked.connect(self.enableNextButton)
self.back2HomeBtn.clicked.connect(self.close)
def showFirstQuestion(self):
print(self.answerSet)
# 根据题目数量加入指定数量button group
self.addBtn()
self.selfAnswerSet = [-1]*self.questionCount
# get question
questionText = ''
# lineIndex = 0
for i in range(self.questionCount):
# linesNumber = int( self.divideSet[i] )
# for lines in self.questionSet[lineIndex: lineIndex + linesNumber]:
# questionText += lines
# lineIndex += linesNumber
questionText += self.questionSet[i]
# 显示文章
_translate = QtCore.QCoreApplication.translate
self.codeBox.setText(_translate("MainWindow", questionText))
self.codeBox.setReadOnly(True)
# 显示选项
for i in range(self.questionCount):
# print(i)
exec("self.choiceBox{0}.setTitle(_translate('MainWindow', '{0}'))".format(i+1))
exec("self.radioButton{0}_a.setText(_translate('MainWindow', '{1}'))".format(i+1, self.answerSet[i][0]))
exec("self.radioButton{0}_b.setText(_translate('MainWindow', '{1}'))".format(i+1, self.answerSet[i][1]))
exec("self.radioButton{0}_c.setText(_translate('MainWindow', '{1}'))".format(i+1, self.answerSet[i][2]))
# exec("self.radioButton{0}_d.setText(_translate('MainWindow', '{1}'))".format(i+1, self.answerSet[i][3]))
# self.previousButton.setEnabled(False)
# self.nextButton.setEnabled(False)
def addBtn(self):
font = QtGui.QFont()
font.setPointSize(12)
# self.choiceBox1.setFont(font)
for i in range(1, self.questionCount+1):
exec("self.choiceGroup{} = QtWidgets.QButtonGroup(self.centralwidget)".format(i))
exec("self.choiceBox{} = QtWidgets.QGroupBox(self.centralwidget)".format(i))
exec("self.choiceBox{0}.setObjectName('choiceBox{0}')".format(i))
exec("self.verticalLayout_4_{0} = QtWidgets.QVBoxLayout(self.choiceBox{0})".format(i))
exec("self.verticalLayout_4_{0}.setObjectName('verticalLayout_4_{0}')".format(i))
exec("self.radioButton{1}_{0} = QtWidgets.QRadioButton(self.choiceBox{1})".format("a", i))
exec("self.radioButton{1}_{0}.setObjectName('radioButton{1}_{0}')".format("a", i))
exec("self.verticalLayout_4_{0}.addWidget(self.radioButton{0}_{1})".format(i, "a"))
exec("self.choiceGroup{1}.addButton(self.radioButton{1}_{0}, 0)".format("a", i))
exec("self.radioButton{1}_{0} = QtWidgets.QRadioButton(self.choiceBox{1})".format("b", i))
exec("self.radioButton{1}_{0}.setObjectName('radioButton{1}_{0}')".format("b", i))
exec("self.verticalLayout_4_{0}.addWidget(self.radioButton{0}_{1})".format(i, "b"))
exec("self.choiceGroup{1}.addButton(self.radioButton{1}_{0}, 1)".format("b", i))
exec("self.radioButton{1}_{0} = QtWidgets.QRadioButton(self.choiceBox{1})".format("c", i))
exec("self.radioButton{1}_{0}.setObjectName('radioButton{1}_{0}')".format("c", i))
exec("self.verticalLayout_4_{0}.addWidget(self.radioButton{0}_{1})".format(i, "c"))
exec("self.choiceGroup{1}.addButton(self.radioButton{1}_{0}, 2)".format("c", i))
# exec("self.radioButton{1}_{0} = QtWidgets.QRadioButton(self.choiceBox{1})".format("d", i))
# exec("self.radioButton{1}_{0}.setObjectName('radioButton{1}_{0}')".format("d", i))
# exec("self.verticalLayout_4_{0}.addWidget(self.radioButton{0}_{1})".format(i, "d"))
# exec("self.choiceGroup{1}.addButton(self.radioButton{1}_{0}, 3)".format("d", i))
exec("self.verticalLayout.addWidget(self.choiceBox{})".format(i))
exec("self.radioButton{}_a.clicked.connect(self.updateAnswer)".format(i))
exec("self.radioButton{}_b.clicked.connect(self.updateAnswer)".format(i))
exec("self.radioButton{}_c.clicked.connect(self.updateAnswer)".format(i))
# exec("self.radioButton{}_d.clicked.connect(self.updateAnswer)".format(i))
def updateAnswer(self):
# answerGoup = []
# answerID = []
for i in range(1, self.questionCount+1):
tmp = eval("self.choiceGroup{}".format(i)).checkedId()
if not tmp == -1:
# answerGoup.append(i-1)
# answerID.append(tmp)
self.selfAnswerSet[i-1] = tmp
questionText = ''
# lineIndex = 0
for i in range(self.questionCount):
# tmpText = ''
# linesNumber = int( self.divideSet[i] )
# for lines in self.questionSet[lineIndex: lineIndex + linesNumber]:
# tmpText += lines
tmpText = self.questionSet[i]
if self.selfAnswerSet[i] != -1:
tmpText = tmpText.replace('___', self.answerSet[i][(self.selfAnswerSet[i])])
try:
tmpText = tmpText.replace('\\', '')
finally:
pass
questionText += tmpText
# lineIndex += linesNumber
# print(self.selfAnswerSet)
_translate = QtCore.QCoreApplication.translate
self.codeBox.setText(_translate("MainWindow", questionText))
# def nextQuestion(self):
# self.previousButton.setEnabled(True)
# print(self.answerID)
# print(self.questionIndex)
# 更新上一题已作答代码内容
# self.answerText = self.questionText
# if self.answerText.find('___') != -1:
# self.answerText = self.answerText.replace('___', self.answerSet[self.questionIndex-1][self.answerID-1])
# try:
# self.finishedText[self.questionIndex - 1] = self.answerText
# except:
# self.finishedText.append(self.answerText)
# # 更新所有已写代码
# _translate = QtCore.QCoreApplication.translate
# codes = ''
# for code in self.finishedText:
# codes += code
# code += self.questionText
# self.codeBox.setPlainText(_translate("MainWindow", codes))
# # print(self.answerText)
# # print(self.finishedText)
# self.questionIndex += 1
# # 更新按键
# # self.btnGroup.
# self.radioButton_a.setChecked(False)
# self.radioButton_b.setChecked(False)
# self.radioButton_c.setChecked(False)
# self.radioButton_d.setChecked(False)
# # radiobutton 有点问题!!!!!!!!!!!!!
# if self.questionIndex > self.questionCount:
# self.hide()
# # self.endWindow = endWindow(self)
# else:
# if self.questionIndex == self.questionCount:
# self.nextButton.setText(_translate("MainWindow", "确定"))
# else:
# self.nextButton.setText(_translate("MainWindow", "下一题"))
# # 更新问题
# linesNumber = int(self.divideSet[self.questionIndex - 1])
# self.questionText = ''
# for lines in self.questionSet[self.lineIndex: self.lineIndex + linesNumber]:
# self.questionText += lines
# self.lineIndex += linesNumber
# self.questionBox.setPlainText(_translate("MainWindow", self.questionText))
# # 更新选项
# self.radioButton_a.setText(_translate("MainWindow", self.answerSet[self.questionIndex-1][0]))
# self.radioButton_b.setText(_translate("MainWindow", self.answerSet[self.questionIndex-1][1]))
# self.radioButton_c.setText(_translate("MainWindow", self.answerSet[self.questionIndex-1][2]))
# self.radioButton_d.setText(_translate("MainWindow", self.answerSet[self.questionIndex-1][3]))
# def previousQuestion(self):
# print(self.questionIndex)
# self.questionIndex -= 1
# print(self.questionIndex)
# # 更新按键
# _translate = QtCore.QCoreApplication.translate
# self.nextButton.setText(_translate("MainWindow", "下一题"))
# if self.questionIndex == 1:
# self.previousButton.setEnabled(False)
# # 更新问题
# oldLinesNumber = int(self.divideSet[self.questionIndex])
# self.lineIndex -= oldLinesNumber
# linesNumber = int(self.divideSet[self.questionIndex-1])
# self.questionText = ''
# for lines in self.questionSet[self.lineIndex - linesNumber: self.lineIndex]:
# self.questionText += lines
# self.questionBox.setPlainText(_translate("MainWindow", self.questionText))
# # 更新选项
# self.radioButton_a.setText(_translate("MainWindow", self.answerSet[self.questionIndex-1][0]))
# self.radioButton_b.setText(_translate("MainWindow", self.answerSet[self.questionIndex-1][1]))
# self.radioButton_c.setText(_translate("MainWindow", self.answerSet[self.questionIndex-1][2]))
# self.radioButton_d.setText(_translate("MainWindow", self.answerSet[self.questionIndex-1][3]))
# def enableNextButton(self):
# self.nextButton.setEnabled(True)
class hardWindow(QMainWindow, Ui_hard.Ui_MainWindow):
"""
填空题模式窗口逻辑
"""
questionSet = []
answerSet = []
# divideSet = []
questionCount = 0
# finishedText = []
quesPosList = []
selfAnswerSet = []
# questionText = ''
# answerText = ''
def __init__(self):
super(hardWindow, self).__init__()
self.setupUi(self)
# # 加入题目文本框
self.highlighter_1 = highlightEditor(self.centralwidget, True)
self.codeBox = self.highlighter_1.editor
self.horizontalLayout_3.addWidget(self.codeBox)
# self.codeBox = QtWidgets.QTextEdit(self.centralwidget)
# self.horizontalLayout_3.addWidget(self.codeBox)
# connection 按键
# self.backButton.clicked.connect(self.close)
# self.nextButton.clicked.connect(self.nextQuestion)
# self.plainTextEdit.textChanged.connect(self.checkNextButton)
# self.codeBox.cursorPositionChanged.connect(self.checkEdibility)
self.back2HomeBtn.clicked.connect(self.close)
def showFirstQuestion(self):
# print(self.questionSet)
# print(self.answerSet)
# print(self.divideSet)
self.addBox()
self.selfAnswerSet = ["___"]*self.questionCount
# get question
questionText = ''
# lineIndex = 0
_translate = QtCore.QCoreApplication.translate
for i in range(self.questionCount):
# text = ''
# linesNumber = int( self.divideSet[i] )
# for lines in self.questionSet[lineIndex: lineIndex + linesNumber]:
# text += lines
# lineIndex += linesNumber
text = self.questionSet[i]
questionText += text
# quesBox显示
exec("self.quesBox{0}.setText(_translate('MainWindow', text))".format(i+1))
self.codeBox.setText(_translate("MainWindow", questionText))
self.codeBox.setReadOnly(True)
# 找到问题所在行和index
# index = -1
# line = -1 ???????为什么不能加????????
for i in range(self.questionCount):
exec("doc = self.quesBox{}.text()".format(i+1))
exec("index = doc.find('___')")
exec("self.quesPosList.append(index)")
exec("self.quesBox{}.cursorPositionChanged.connect(self.checkEdibility)".format(i+1))
# print(self.quesPosList)
def addBox(self):
font = QtGui.QFont()
font.setPointSize(12)
# self.choiceBox1.setFont(font)
for i in range(1, self.questionCount+1):
exec("highlighter_{} = highlightEditor(self.scrollAreaWidgetContents)".format(i))
exec("self.quesBox{0} = highlighter_{0}.editor".format(i))
exec("self.quesBox{0}.setObjectName('quesBox{0}')".format(i))
exec("self.verticalLayout.addWidget(self.quesBox{})".format(i))
def checkEdibility(self): #记得改成quesBox!!!
# print(1)
# beginPos = 0
# quesPos = []
# while not( doc.find("___", beginPos).isNull() ):
# pos = doc.find("___", beginPos).position()
# quesPos.append(pos)
# beginPos = pos
# print(quesPos)
# curPos = tc.position()
# for pos in quesPos:
# if not (curPos < pos-3 or curPos > pos):
# self.codeBox.setReadOnly(False)
# return
# else:
# self.codeBox.setReadOnly(True)
exec("text = ''")
for i in range(self.questionCount):
# exec("tc = self.quesBox{}.textCursor()".format(i+1))
# exec("textLine = tc.block().firstLineNumber()")
# exec("self.quesBox{0}.setReadOnly(False) if textLine == self.quesPosList[i][1] else self.quesBox{0}.setReadOnly(True) ".format(i+1))
# refresh my answer
exec("tmpText = self.quesBox{}.text()".format(i+1))
exec("startIndex = self.quesPosList[i]")
exec("endIndex = tmpText.find('\\n', startIndex)")
# exec("print(startIndex)")
# exec("print(endIndex)")
exec("self.selfAnswerSet[i] = tmpText[startIndex: endIndex]")
# exec("print(self.selfAnswerSet)")
# new add: find answer by difflib of python !!!!!!!!!!!!!!!!!!!!!!!
exec("text += tmpText")
# refresh codeBox
_translate = QtCore.QCoreApplication.translate
exec("self.codeBox.setText(_translate('MainWindow', text))")
# def checkNextButton(self):
# codes = self.plainTextEdit.toPlainText()
# if codes.find("__") == -1:
# self.nextButton.setEnabled(True)
# def nextQuestion(self):
# 改为导出键??
class menu():
"""
菜单栏逻辑
"""
def __init__(self, window):
self.window = window
self.workingDir = root + '/CourseCode'
self.fileName = ""
self.mode = 0
self.window.importBtn.clicked.connect(self.openFile)
self.window.exportCodeBtn.clicked.connect(self.exportCode)
self.window.exportAnswerBtn.clicked.connect(self.exportAnswer)
def openFile(self):
"""
导入文件
"""
_translate = QtCore.QCoreApplication.translate
fileName, fileType = QFileDialog.getOpenFileName(
self.window, "选择文件", self.workingDir, f"Answer File (*.ans{self.mode});;All Files (*)")
if fileName:
fileHandle = open(fileName, 'r', encoding='utf-8')
codes = fileHandle.read()
fileHandle.close()
tmpList = codes.split('@')
# print(tmpList)
# 选择题
if isinstance(self.window, easyWindow):
for i in range(len(tmpList)):
self.window.selfAnswerSet[i] = int(tmpList[i])
# 显示
questionText = ''
# lineIndex = 0
for i in range(self.window.questionCount):
# tmpText = ''
# linesNumber = int( self.window.divideSet[i] )
# for lines in self.window.questionSet[lineIndex: lineIndex + linesNumber]:
# tmpText += lines
tmpText = self.window.questionSet[i]
if self.window.selfAnswerSet[i] != -1:
tmpText = tmpText.replace('___', self.window.answerSet[i][(self.window.selfAnswerSet[i])])
try:
tmpText = tmpText.replace('\\', '')
finally:
pass
questionText += tmpText
# lineIndex += linesNumber
# print(self.window.selfAnswerSet)
_translate = QtCore.QCoreApplication.translate
self.window.codeBox.setText(_translate("MainWindow", questionText))
# self.window.codeBox.setText(_translate("MainWindow", codes))
elif isinstance(self.window, hardWindow):
# get question
for i in range(len(tmpList)):
self.window.selfAnswerSet[i] = tmpList[i]
for i in range(self.window.questionCount):
exec("self.window.quesBox{}.cursorPositionChanged.disconnect(self.window.checkEdibility)".format(i+1))
# print(self.window.selfAnswerSet)
exec("""questionText = ''
_translate = QtCore.QCoreApplication.translate
for i in range(self.window.questionCount):
text = self.window.questionSet[i]
print(text)
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
startIndex = self.window.quesPosList[i]
endIndex = text.find('\\n', startIndex)
print(startIndex)
print(endIndex)
print(self.window.selfAnswerSet)
text = text[:startIndex] + self.window.selfAnswerSet[i] + text[endIndex:]
print(text)
print('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
exec("self.window.quesBox{}.setText(_translate('MainWindow', text))".format(i+1))
questionText += text
self.window.codeBox.setText(_translate("MainWindow", questionText))
self.window.codeBox.setReadOnly(True)""")
else:
# 若用户关闭对话框或选择“取消”
pass
def exportCode(self):
"""
导出代码
"""
codes = self.window.codeBox.text()
codes = codes.replace('\r', '')
fileName = QFileDialog.getSaveFileName(
self.window, "保存文件", self.workingDir, "Python File(*.py);;All Files (*)")[0]
if fileName:
self.fileName = fileName
self.save(fileName, codes)
else:
# 若用户关闭对话框或选择“取消”
pass
def exportAnswer(self):
"""
导出答案
"""
answers = '@'.join(map(str, self.window.selfAnswerSet)) #TODO: answerSet替换成答案集
fileName = QFileDialog.getSaveFileName(
self.window, "保存文件", self.workingDir, f"Answer File(*.ans{self.mode});;All Files (*)")[0]
if fileName:
self.fileName = fileName
self.save(fileName, answers)
else:
# 若用户关闭对话框或选择“取消”
pass
def save(self, fileName, content):
"""
文件保存模块
"""
fileHandle = open(fileName, 'w', encoding='utf-8')
fileHandle.write(content)
fileHandle.close()
self.window.statusbar.showMessage("已保存", 2000)
# class menu(QMenu):
# """
# 菜单栏逻辑
# """
# def __init__(self, window):
# super(menu, self).__init__()
# self.window = window
# self.workingDir = os.path.split(os.path.abspath(__file__))[0]
# self.fileName = ""
# self.window.importAction.triggered.connect(self.openFile)
# self.window.exportAction.triggered.connect(self.export)
# self.window.saveAction.triggered.connect(self.saveFile)
# self.window.quitAction.triggered.connect(qApp.quit)
# def openFile(self):
# """
# 导入文件
# """
# _translate = QtCore.QCoreApplication.translate
# fileName, fileType = QFileDialog.getOpenFileName(
# self, "选择文件", self.workingDir, "Python File (*.py);;All Files (*)")
# if fileName:
# fileHandle = open(fileName, 'r', encoding='utf-8')
# codes = fileHandle.read()
# fileHandle.close()
# self.window.codeBox.setPlainText(_translate("MainWindow", codes))
# else:
# # 若用户关闭对话框或选择“取消”
# pass
# def export(self):
# """
# 导出文件
# """
# fileName = QFileDialog.getSaveFileName(
# self, "保存文件", self.workingDir, "Python File(*.py);;All Files (*)")[0]
# if fileName:
# self.fileName = fileName
# self.save(fileName)
# else:
# # 若用户关闭对话框或选择“取消”
# pass
# def saveFile(self):
# """
# 保存文件
# """
# if self.fileName:
# self.save(self.fileName)
# else:
# self.export()
# def save(self, fileName):
# """
# 文件保存模块
# """
# codes = self.window.codeBox.toPlainText()
# fileHandle = open(fileName, 'w', encoding='utf-8')
# fileHandle.write(codes)
# fileHandle.close()
# self.window.statusbar.showMessage("已保存", 2000)