This repository was archived by the owner on Aug 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPS.pyw
More file actions
1555 lines (1207 loc) · 67.6 KB
/
Copy pathPS.pyw
File metadata and controls
1555 lines (1207 loc) · 67.6 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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2026 Iviesever
# The MIT License (MIT)
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# github: https://github.com/Iviesever/Python-practice-questions
import tkinter as tk
from tkinter import messagebox, ttk, simpledialog
import tkinter.font as tkfont
import re
import os
import json
import glob
import random
import time
from datetime import datetime
class ConfigManager:
def __init__(self, filename="config.json"):
self.filename = filename
self.defaults = {
"font_size_base": 12,
"font_size_title": 16,
"btn_scale": 1.0,
"auto_submit": True,
"confirm_exit": True,
"file_selection_mode": "remembered", # 可选值: "all" (全选) 或 "remembered" (记忆)
"last_repo": "", # 记录上次题库
"last_files": [], # 记录上次选中的文件名列表
"mistake_operator": "不启用", # 默认不启用
"mistake_value": "1" # 默认次数
}
self.config = self.defaults.copy()
self.load()
def load(self):
if os.path.exists(self.filename):
try:
with open(self.filename, 'r', encoding='utf-8') as f:
loaded = json.load(f)
self.config.update(loaded)
except:
pass
def save(self):
with open(self.filename, 'w', encoding='utf-8') as f:
json.dump(self.config, f, indent=2)
def get(self, key):
return self.config.get(key, self.defaults.get(key))
def set(self, key, value):
self.config[key] = value
self.save()
class Question:
def __init__(self, q_type, content, options, correct_answer, raw_text, source_file):
self.q_type = q_type
self.content = content
self.options = options
self.correct_answer = correct_answer
self.raw_text = raw_text
self.source_file = source_file
def get_id(self):
clean_content = re.sub(r'\W', '', self.content)
return clean_content[:50]
class QuestionParser:
@staticmethod
def parse_target_files(file_list):
questions = []
for filepath in file_list:
if os.path.exists(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
questions.extend(QuestionParser.parse_text(f.read(), os.path.basename(filepath)))
return questions
@staticmethod
def parse_text(text, filename="unknown"):
# 清洗底部统计区
lines = text.split('\n')
clean_lines = []
for line in lines:
if re.search(r'一\.\s*单选题.*?\n\s*1\s+2\s+3', line) or re.match(r'^1\s+2\s+3\s+4', line): break
if re.match(r'^\s*AI讲解\s*$', line) or re.match(r'^\s*\d+(\.\d+)?分\s*$', line): continue
clean_lines.append(line)
text = "\n".join(clean_lines)
parsed_questions = []
pattern_header = re.compile(r'^\s*(\d+)[\.\、]\s*', re.MULTILINE)
matches = list(pattern_header.finditer(text))
for i in range(len(matches)):
start_idx = matches[i].start()
end_idx = matches[i+1].start() if i + 1 < len(matches) else len(text)
block = text[start_idx:end_idx]
q = QuestionParser.parse_single_block(block, filename)
if q: parsed_questions.append(q)
return parsed_questions
@staticmethod
def parse_single_block(block, filename):
try:
q_type = 'single'
if '多选' in block[:50]: q_type = 'multi'
elif '判断' in block[:50]: q_type = 'judge'
elif '填空' in block[:50]: q_type = 'fill'
content = re.sub(r'^\s*\d+[\.\、]\s*', '', block).strip()
content = re.sub(r'[\(\[\uff08]\s*(?:单选题|多选题|判断题|填空题|.*?分).*?[\)\]\uff09]', '', content)
stop_patterns = [r'(?:^|\n)\s*[A-Z][\.\、]', r'我的答案', r'正确答案', r'AI讲解', r'\d+分', r'\(1\)']
min_split = len(content)
for pat in stop_patterns:
m = re.search(pat, content)
if m: min_split = min(min_split, m.start())
q_content = content[:min_split].strip()
if not q_content: return None
options = []
if q_type in ['single', 'multi']:
opt_iter = re.finditer(r'(?:^|\n)\s*([A-Z])[\.\、]\s*(.*?)(?=\n\s*[A-Z][\.\、]|\n\s*我的答案|\n\s*正确答案|\n\s*\d+分|$)', block, re.DOTALL)
for m in opt_iter: options.append(f"{m.group(1)}. {m.group(2).strip()}")
elif q_type == 'judge': options = ["A. 对", "B. 错"]
ans_match = re.search(r'正确答案[::]\s*(?:[\r\n]+)?(.*)', block, re.DOTALL)
correct_ans = ""
if ans_match:
raw = ans_match.group(1).strip()
end_of_ans = len(raw)
for stop in ['AI讲解', '\n', '1. ']:
idx = raw.find(stop);
if idx != -1: end_of_ans = min(end_of_ans, idx)
raw_clean = raw[:end_of_ans].strip()
if q_type == 'judge':
if '对' in raw_clean or 'T' in raw_clean: correct_ans = 'A'
elif '错' in raw_clean or 'F' in raw_clean: correct_ans = 'B'
else: correct_ans = raw_clean
elif q_type == 'fill': correct_ans = re.sub(r'^\(\d+\)\s*', '', raw_clean)
else: correct_ans = "".join(re.findall(r'[A-Z]', raw_clean))
if q_type in ['single', 'multi'] and not options: return None
return Question(q_type, q_content, options, correct_ans, block, filename)
except: return None
class MistakeManager:
def __init__(self, filename="mistakes.json"):
self.filename = filename
self.data = {}
self.load()
def load(self):
if os.path.exists(self.filename):
try:
with open(self.filename, 'r', encoding='utf-8') as f: self.data = json.load(f)
except: self.data = {}
def save(self):
with open(self.filename, 'w', encoding='utf-8') as f: json.dump(self.data, f, ensure_ascii=False, indent=2)
def add_mistake(self, q_id):
self.data[q_id] = self.data.get(q_id, 0) + 1
self.save()
def get_count(self, q_id): return self.data.get(q_id, 0)
def get_max_errors(self): return max(self.data.values()) if self.data else 0
# 新增考试配置和历史记录管理类
class ExamConfigManager:
"""管理考试配置预设 (题目数量、分值、时间)"""
def __init__(self, filename="exam_presets.json"):
self.filename = filename
self.presets = {}
self.load()
def load(self):
if os.path.exists(self.filename):
try:
with open(self.filename, 'r', encoding='utf-8') as f:
self.presets = json.load(f)
except: self.presets = {}
def save(self):
with open(self.filename, 'w', encoding='utf-8') as f:
json.dump(self.presets, f, indent=2)
def get_preset(self, name):
return self.presets.get(name, None)
def save_preset(self, name, data):
self.presets[name] = data
self.save()
def get_names(self):
return list(self.presets.keys())
# 管理考试历史记录
class ExamHistoryManager:
def __init__(self, filename="exam_history.json"):
self.filename = filename
self.history = {} # { "repo_name": [record1, record2...] }
self.load()
def load(self):
if os.path.exists(self.filename):
try:
with open(self.filename, 'r', encoding='utf-8') as f:
self.history = json.load(f)
except: self.history = {}
def save(self):
with open(self.filename, 'w', encoding='utf-8') as f:
json.dump(self.history, f, indent=2, ensure_ascii=False)
def add_record(self, repo_name, record):
if repo_name not in self.history:
self.history[repo_name] = []
self.history[repo_name].insert(0, record) # 新的在前
self.save()
def get_records(self, repo_name):
return self.history.get(repo_name, [])
class QuizApp:
def __init__(self, root):
self.root = root
self.root.title("Python 刷题")
if os.path.exists("favicon.ico"):
try:
self.root.iconbitmap("favicon.ico")
except:
pass
self.root.geometry("1000x800")
self.cfg = ConfigManager()
self.mistake_mgr = MistakeManager()
# 初始化管理器
self.exam_cfg_mgr = ExamConfigManager()
self.exam_hist_mgr = ExamHistoryManager()
self.exam_mode = False # 标记当前是否在模拟考试中
self.exam_timer_id = None
self.exam_end_time = 0
self.txt_files = glob.glob("*.txt")
self.all_questions_cache = QuestionParser.parse_target_files(self.txt_files)
self.session_stats = {'total': 0, 'correct': 0}
self.current_queue = []
self.current_index = 0
self.current_q = None
self.type_map = {'single': '单选题', 'multi': '多选题', 'judge': '判断题', 'fill': '填空题'}
self.filter_vars = {
'single': tk.BooleanVar(value=True),
'multi': tk.BooleanVar(value=True),
'judge': tk.BooleanVar(value=True),
'fill': tk.BooleanVar(value=True)
}
self.setup_menu()
def get_fonts(self):
base = self.cfg.get("font_size_base")
title = self.cfg.get("font_size_title")
return {
"title": ("微软雅黑", title, "bold"),
"normal": ("微软雅黑", base),
"ui": ("微软雅黑", base - 2),
"btn": ("微软雅黑", base)
}
def show_settings_page(self):
"""显示设置页面(覆盖主窗口)"""
self.clear_window() # 清空当前窗口内容
# 定义动态字体对象(仅用于预览)
f_title_obj = tkfont.Font(family="微软雅黑", size=self.cfg.get("font_size_title"), weight="bold")
f_base_obj = tkfont.Font(family="微软雅黑", size=self.cfg.get("font_size_base"))
# 顶部返回栏
top_bar = tk.Frame(self.root, bg="#eee", pady=10)
top_bar.pack(fill="x", side="top")
tk.Button(top_bar, text="< 返回", command=self.setup_menu, font=f_base_obj).pack(side="left", padx=10)
tk.Label(top_bar, text="设置", font=("微软雅黑", 16, "bold"), bg="#eee").pack(side="left", padx=10)
# 主滚动区域
canvas = tk.Canvas(self.root)
scrollbar = tk.Scrollbar(self.root, orient="vertical", command=canvas.yview)
scroll_frame = tk.Frame(canvas, padx=20, pady=20)
scroll_frame.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
canvas.create_window((0, 0), window=scroll_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# 字体设置
def update_title_font(val):
f_title_obj.configure(size=int(val))
def update_base_font(val):
f_base_obj.configure(size=int(val))
tk.Label(scroll_frame, text="题目字号", font=f_base_obj).pack(pady=10, anchor="w")
scale_font = tk.Scale(scroll_frame, from_=3, to=30, orient=tk.HORIZONTAL, length=300,
command=update_title_font)
scale_font.set(self.cfg.get("font_size_title"))
scale_font.pack(anchor="w")
tk.Label(scroll_frame, text="字号预览", font=f_title_obj, fg="#1976d2").pack(pady=5, anchor="w")
tk.Label(scroll_frame, text="选项/按钮大小", font=f_base_obj).pack(pady=10, anchor="w")
scale_base = tk.Scale(scroll_frame, from_=3, to=30, orient=tk.HORIZONTAL, length=300,
command=update_base_font)
scale_base.set(self.cfg.get("font_size_base"))
scale_base.pack(anchor="w")
ttk.Separator(scroll_frame, orient='horizontal').pack(fill='x', pady=20)
# 选项设置
var_auto = tk.BooleanVar(value=self.cfg.get("auto_submit"))
chk_auto = tk.Checkbutton(scroll_frame, text="单选/判断题点击选项直接判分", variable=var_auto, font=f_base_obj)
chk_auto.pack(pady=5, anchor="w")
var_exit = tk.BooleanVar(value=self.cfg.get("confirm_exit") if self.cfg.get("confirm_exit") is not None else True)
chk_exit = tk.Checkbutton(scroll_frame, text="返回主页时显示确认提示", variable=var_exit, font=f_base_obj)
chk_exit.pack(pady=5, anchor="w")
ttk.Separator(scroll_frame, orient='horizontal').pack(fill='x', pady=20)
# 题库加载
tk.Label(scroll_frame, text="切换/加载题库时的默认行为:", font=f_base_obj).pack(pady=(0, 5), anchor="w")
current_mode = self.cfg.get("file_selection_mode")
if current_mode not in ["all", "remembered"]: current_mode = "remembered"
var_sel_mode = tk.StringVar(value=current_mode)
tk.Radiobutton(scroll_frame, text="记忆上次选中的文件", variable=var_sel_mode, value="remembered", font=f_base_obj).pack(pady=2, anchor="w")
tk.Radiobutton(scroll_frame, text="默认全选所有文件", variable=var_sel_mode, value="all", font=f_base_obj).pack(pady=2, anchor="w")
def save_conf():
self.cfg.set("font_size_title", scale_font.get())
self.cfg.set("font_size_base", scale_base.get())
self.cfg.set("auto_submit", var_auto.get())
self.cfg.set("confirm_exit", var_exit.get())
self.cfg.set("file_selection_mode", var_sel_mode.get())
messagebox.showinfo("提示", "设置已保存!")
self.setup_menu() # 保存后直接返回主页
# 底部
tk.Button(scroll_frame, text="保存设置并返回主页", command=save_conf,
bg="#4caf50", fg="white", font=f_base_obj, height=2).pack(pady=30, fill="x")
# 适配手机屏幕布局
def setup_menu(self):
self.clear_window()
fonts = self.get_fonts()
self.current_q = None
# 主容器
main_frame = tk.Frame(self.root, padx=10, pady=10) # 减小边距
main_frame.pack(expand=True, fill="both")
# 顶部标题
header_frame = tk.Frame(main_frame)
header_frame.pack(fill="x", pady=5)
tk.Label(header_frame, text="Python 刷题", font=("微软雅黑", 18, "bold")).pack(side="left")
tk.Button(header_frame, text="设置", command=self.show_settings_page, font=fonts["ui"]).pack(side="right")
# 0. 切换题库
repo_frame = tk.LabelFrame(main_frame, text="0. 切换题库", font=fonts["normal"], padx=5, pady=5)
repo_frame.pack(fill="x", pady=5)
base_dir = "题库"
if not os.path.exists(base_dir): os.makedirs(base_dir)
repos = [d for d in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, d))]
if not repos:
tk.Label(repo_frame, text="未发现题库文件夹!", fg="red", font=fonts["normal"]).pack()
self.combo_repo = None
else:
self.combo_repo = ttk.Combobox(repo_frame, values=repos, state="readonly", font=fonts["normal"])
self.combo_repo.pack(fill="x", padx=5) # 让下拉框填满宽度
last_repo = self.cfg.get("last_repo")
if last_repo and last_repo in repos:
self.combo_repo.set(last_repo)
else:
self.combo_repo.current(0)
self.combo_repo.bind("<<ComboboxSelected>>", self.refresh_file_list)
# 1. 选择题目文件
file_frame_title = "1. 选择题目文件"
file_frame = tk.LabelFrame(main_frame, text=file_frame_title, font=fonts["normal"], padx=5, pady=5)
file_frame.pack(fill="x", pady=5, expand=True)
# 工具栏分成上下两行,防止手机上挤在一起
toolbar_frame = tk.Frame(file_frame)
toolbar_frame.pack(fill="x", pady=(0, 5))
# 第一行:全选按钮
def toggle_select_all():
if self.file_listbox.size() == 0: return
if len(self.file_listbox.curselection()) == self.file_listbox.size():
self.file_listbox.selection_clear(0, tk.END)
else:
self.file_listbox.select_set(0, tk.END)
# 右侧:全选按钮 (先 pack 右边,防止被左边挤出屏幕)
btn_select_all = tk.Button(toolbar_frame, text="全选", command=toggle_select_all,
font=("微软雅黑", 10), bg="#eee", padx=10)
btn_select_all.pack(side="right", anchor="e")
# 左侧:错题控制区 (放入一个 Frame 整体靠左)
mistake_frame = tk.Frame(toolbar_frame)
mistake_frame.pack(side="left", fill="x")
# 标签:"错题" (红色) - 去掉"模式"二字以节省空间
tk.Label(mistake_frame, text="错题", font=fonts["normal"], fg="#d32f2f").pack(side="left", padx=(0, 2))
# 下拉框 1:操作符 (宽度6)
self.combo_mistake_op = ttk.Combobox(mistake_frame, values=["不启用", ">=", "=="],
width=6, state="readonly", font=fonts["normal"])
self.combo_mistake_op.pack(side="left", padx=2)
# 读取配置
op_mem = self.cfg.get("mistake_operator")
self.combo_mistake_op.set(op_mem if op_mem in ["不启用", ">=", "=="] else "不启用")
# 下拉框 2:次数 (宽度设为 3,够显示 999)
self.combo_mistake_val = ttk.Combobox(mistake_frame, width=3, state="readonly", font=fonts["normal"])
self.combo_mistake_val.pack(side="left", padx=2)
# 标签:"次"
tk.Label(mistake_frame, text="次", font=fonts["normal"]).pack(side="left", padx=(0, 2))
# 绑定保存配置事件
def save_mistake_config(event):
self.cfg.set("mistake_operator", self.combo_mistake_op.get())
self.cfg.set("mistake_value", self.combo_mistake_val.get())
self.combo_mistake_op.bind("<<ComboboxSelected>>", save_mistake_config)
self.combo_mistake_val.bind("<<ComboboxSelected>>", save_mistake_config)
# 列表框
list_scroll = tk.Scrollbar(file_frame)
list_scroll.pack(side="right", fill="y")
self.file_listbox = tk.Listbox(file_frame, selectmode=tk.MULTIPLE,
height=10, # 这里控制文件框里显示的行数
font=("Consolas", self.cfg.get("font_size_base")),
yscrollcommand=list_scroll.set,
exportselection=False) # 防止点击别处时丢失勾选
self.file_listbox.pack(side="left", fill="both", expand=True)
list_scroll.config(command=self.file_listbox.yview)
# 2. 题型过滤
filter_frame = tk.LabelFrame(main_frame, text="2. 题型过滤", font=fonts["normal"], padx=5, pady=5)
filter_frame.pack(fill="x", pady=5)
# 使用 Grid 网格布局 (2行2列),防止单行排不下
filter_frame.grid_columnconfigure(0, weight=1)
filter_frame.grid_columnconfigure(1, weight=1)
type_display = [('单选题', 'single'), ('多选题', 'multi'), ('判断题', 'judge'), ('填空题', 'fill')]
for i, (txt, key) in enumerate(type_display):
chk = tk.Checkbutton(filter_frame, text=txt, variable=self.filter_vars[key], font=fonts["normal"])
# i//2 计算行号(0,0,1,1), i%2 计算列号(0,1,0,1)
chk.grid(row=i//2, column=i%2, sticky="w", padx=10)
# 3. 开始练习
action_frame = tk.LabelFrame(main_frame, text="3. 开始练习", font=fonts["normal"], padx=5, pady=5)
action_frame.pack(fill="x", pady=5)
# 按钮去除固定宽度,使用 expand=True 自动填满
tk.Button(action_frame, text="顺序练习", height=2, font=fonts["btn"], bg="#e3f2fd",
command=lambda: self.start_selected_practice(shuffle=False)).pack(side="left", expand=True, fill="x", padx=2)
tk.Button(action_frame, text="随机练习", height=2, font=fonts["btn"], bg="#e3f2fd",
command=lambda: self.start_selected_practice(shuffle=True)).pack(side="left", expand=True, fill="x", padx=2)
# 4. 模拟考试
exam_frame = tk.LabelFrame(main_frame, text="4. 模拟考试", font=fonts["normal"], padx=5, pady=5)
exam_frame.pack(fill="x", pady=5)
# 按钮自动填满
tk.Button(exam_frame, text="配置考试", height=2, bg="#fff9c4", font=fonts["btn"],
command=self.show_exam_config_page).pack(side="left", expand=True, fill="x", padx=2)
tk.Button(exam_frame, text="考试记录", height=2, bg="#f0f0f0", font=fonts["btn"],
command=self.show_exam_history_page).pack(side="left", expand=True, fill="x", padx=2)
if getattr(self, 'combo_repo', None):
self.refresh_file_list()
def refresh_file_list(self, event=None):
if not getattr(self, 'combo_repo', None): return
if not self.combo_repo.winfo_exists(): return
current_repo = self.combo_repo.get()
self.file_listbox.delete(0, tk.END)
search_path = os.path.join("题库", current_repo, "*.txt")
self.current_txt_paths = glob.glob(search_path)
# 定义自然排序的 key 函数
def natural_key(text):
return [int(c) if c.isdigit() else c.lower() for c in re.split(r'(\d+)', text)]
# 对获取到的路径列表进行排序
# 这里对 os.path.basename(p) 排序,即根据文件名排序,而不是全路径
self.current_txt_paths.sort(key=lambda p: natural_key(os.path.basename(p)))
# 用于映射:文件名 -> Listbox索引
name_to_index = {}
if not self.current_txt_paths:
self.file_listbox.insert(tk.END, "(该文件夹下无txt文件)")
self.all_questions_cache = []
else:
for idx, p in enumerate(self.current_txt_paths):
fname = os.path.basename(p)
self.file_listbox.insert(tk.END, fname)
name_to_index[fname] = idx
self.all_questions_cache = QuestionParser.parse_target_files(self.current_txt_paths)
# 根据模式执行全选或恢复记忆
mode = self.cfg.get("file_selection_mode")
last_repo = self.cfg.get("last_repo")
last_files = self.cfg.get("last_files") or []
# 模式A: 强制全选
if mode == "all":
self.file_listbox.select_set(0, tk.END)
# 模式B: 记忆恢复 (只有当 repo 匹配时才恢复)
elif mode == "remembered":
if current_repo == last_repo:
missing_files = []
for f_name in last_files:
if f_name in name_to_index:
self.file_listbox.select_set(name_to_index[f_name])
else:
missing_files.append(f_name)
if missing_files:
msg = "以下上次选中的文件未找到(可能已被删除或重命名):\n\n" + "\n".join(missing_files)
messagebox.showwarning("文件丢失提示", msg)
else:
# 如果是“记忆模式”但切到了一个新题库(没有记忆),默认什么都不选
# 如果想在这里也默认全选,可以把下面的 pass 改成 self.file_listbox.select_set(0, tk.END)
pass
# 计算错题最大值
local_max_err = 0
if self.all_questions_cache:
for q in self.all_questions_cache:
cnt = self.mistake_mgr.get_count(q.get_id())
if cnt > local_max_err: local_max_err = cnt
# 更新第二个下拉框(次数)
if getattr(self, 'combo_mistake_val', None) and self.combo_mistake_val.winfo_exists():
# --- 改动开始:确保列表至少包含 "1",并且从 1 开始到 max ---
# 如果最大错误数是 0,也显示 1,方便用户预设
limit = max(1, local_max_err)
vals = [str(i) for i in range(1, limit + 1)]
self.combo_mistake_val['values'] = vals
# --- 改动结束 ---
# 尝试恢复记忆的值
mem_val = self.cfg.get("mistake_value")
if mem_val in vals:
self.combo_mistake_val.set(mem_val)
else:
# 如果记忆无效,默认选第1个(也就是 "1")
if vals: self.combo_mistake_val.current(0)
def get_allowed_types(self):
return [k for k, v in self.filter_vars.items() if v.get()]
def start_selected_practice(self, shuffle):
idxs = self.file_listbox.curselection()
if not idxs: return messagebox.showwarning("提示", "请先勾选至少一个题库文件!")
selected_filenames = [self.file_listbox.get(i) for i in idxs]
if "(该文件夹下无txt文件)" in selected_filenames:
return
# 保存配置
if getattr(self, 'combo_repo', None):
repo_dir = self.combo_repo.get()
self.cfg.set("last_repo", repo_dir)
self.cfg.set("last_files", selected_filenames)
# 重新构建完整路径用于解析(如果没有缓存机制,就用这个;如果有all_questions_cache,就用下面的过滤)
files = [os.path.join("题库", repo_dir, fname) for fname in selected_filenames]
qs = QuestionParser.parse_target_files(files)
allowed = self.get_allowed_types()
qs = [q for q in qs if q.q_type in allowed]
op = self.combo_mistake_op.get()
target_val_str = self.combo_mistake_val.get()
if op != "不启用":
if not target_val_str.isdigit():
return messagebox.showwarning("提示", "请选择有效的错题次数!")
target_val = int(target_val_str)
filtered_qs = []
for q in qs:
err_count = self.mistake_mgr.get_count(q.get_id())
if op == "==":
if err_count == target_val: filtered_qs.append(q)
elif op == ">=":
if err_count >= target_val: filtered_qs.append(q)
qs = filtered_qs
if not qs:
return messagebox.showinfo("提示", f"在所选文件中,没有找到符合“错误 {op} {target_val} 次”的题目。")
if not qs: return messagebox.showerror("错误", "所选条件(文件/题型/错题)下没有题目!")
self.start_quiz(qs, shuffle)
def start_mistake_review(self):
val = self.combo_mistake.get()
if val in ["无", ""]: return
# 获取列表框中选中的文件名
idxs = self.file_listbox.curselection()
if not idxs:
messagebox.showwarning("提示", "请先在左侧列表中勾选要复习的题目文件!")
return
selected_files = set(self.file_listbox.get(i) for i in idxs)
allowed = self.get_allowed_types()
qs = []
if val == "所有":
# 筛选:属于选中文件 + 错误次数大于0 + 题型匹配
qs = [
q for q in self.all_questions_cache
if q.source_file in selected_files
and self.mistake_mgr.get_count(q.get_id()) > 0
and q.q_type in allowed
]
else:
# 筛选:属于选中文件 + 错误次数等于特定值 + 题型匹配
try:
target_count = int(val)
qs = [
q for q in self.all_questions_cache
if q.source_file in selected_files
and self.mistake_mgr.get_count(q.get_id()) == target_count
and q.q_type in allowed
]
except ValueError:
return # 防止解析错误
if not qs:
messagebox.showinfo("提示", "在所选文件和题型中,没有找到符合条件的错题。")
return
self.start_quiz(qs, shuffle=True)
# 考试配置页面
def show_exam_config_page(self):
# 1. 检查文件选择
idxs = self.file_listbox.curselection()
if not idxs: return messagebox.showwarning("提示", "请先在“1. 选择题目文件”中勾选作为考试范围的题库!")
selected_files = [self.file_listbox.get(i) for i in idxs]
# 2. 统计可用题数
if getattr(self, 'combo_repo', None):
repo_dir = self.combo_repo.get()
files_path = [os.path.join("题库", repo_dir, fname) for fname in selected_files]
else: return
all_qs = QuestionParser.parse_target_files(files_path)
type_counts = {'single': 0, 'multi': 0, 'judge': 0, 'fill': 0}
for q in all_qs:
if q.q_type in type_counts: type_counts[q.q_type] += 1
# 3. 读取上次记忆的配置
last_config = self.cfg.get("last_exam_settings") or {}
# 4. 构建UI (上下滚动布局,适应手机)
self.clear_window()
fonts = self.get_fonts()
# 顶部栏
top_bar = tk.Frame(self.root, bg="#eee", pady=10); top_bar.pack(fill="x")
tk.Button(top_bar, text="< 返回", command=self.setup_menu, font=fonts["ui"]).pack(side="left", padx=10)
tk.Label(top_bar, text="考试配置", font=("微软雅黑", 16, "bold"), bg="#eee").pack(side="left", padx=10)
# 滚动区域
canvas = tk.Canvas(self.root, bg="white")
scrollbar = tk.Scrollbar(self.root, orient="vertical", command=canvas.yview)
scroll_content = tk.Frame(canvas, bg="white", padx=15, pady=15)
scroll_content.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
canvas_window = canvas.create_window((0, 0), window=scroll_content, anchor="nw")
# 让内容宽度自适应屏幕
def on_resize(event):
canvas.itemconfig(canvas_window, width=event.width)
canvas.bind("<Configure>", on_resize)
canvas.configure(yscrollcommand=scrollbar.set)
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# 题目参数设置 (使用 Grid)
tk.Label(scroll_content, text="题目设置", font=fonts["title"], bg="white").pack(anchor="w", pady=(0, 10))
grid_frame = tk.Frame(scroll_content, bg="white")
grid_frame.pack(fill="x")
# 设置列宽权重,保证拉伸
for c in range(4): grid_frame.columnconfigure(c, weight=1)
# 表头
headers = ["题型", "可用", "考题数", "分值"]
for i, h in enumerate(headers):
tk.Label(grid_frame, text=h, font=("微软雅黑", 10, "bold"), fg="#555", bg="white").grid(row=0, column=i, pady=5)
self.exam_entries = {}
row = 1
for q_type in ['single', 'multi', 'judge', 'fill']:
t_name = self.type_map[q_type]
avail = type_counts[q_type]
# 题型名称
tk.Label(grid_frame, text=t_name, font=fonts["normal"], bg="white").grid(row=row, column=0, pady=8)
# 可用数量
tk.Label(grid_frame, text=f"({avail})", font=fonts["ui"], fg="#888", bg="white").grid(row=row, column=1, pady=8)
# 题目数量输入 (尝试加载记忆值)
default_count = 0
if q_type in last_config:
default_count = int(last_config[q_type].get('count', 0))
e_count = tk.Entry(grid_frame, width=5, font=fonts["normal"], justify="center", bg="#f5f5f5")
e_count.insert(0, str(default_count))
e_count.grid(row=row, column=2, pady=8)
# 分值输入 (尝试加载记忆值)
default_score = 1 if q_type=='single' else 2
if q_type in last_config:
default_score = float(last_config[q_type].get('score', default_score))
e_score = tk.Entry(grid_frame, width=5, font=fonts["normal"], justify="center", bg="#f5f5f5")
e_score.insert(0, str(default_score))
e_score.grid(row=row, column=3, pady=8)
self.exam_entries[q_type] = {'count': e_count, 'score': e_score, 'avail': avail}
row += 1
ttk.Separator(scroll_content, orient='horizontal').pack(fill='x', pady=20)
# 时间设置
time_frame = tk.Frame(scroll_content, bg="white")
time_frame.pack(fill="x")
tk.Label(time_frame, text="考试限时 (分钟):", font=fonts["normal"], bg="white").pack(side="left")
# 尝试加载时间记忆
last_time = last_config.get('time', 60)
e_time = tk.Entry(time_frame, width=8, font=fonts["normal"], justify="center", bg="#f5f5f5")
e_time.insert(0, str(last_time))
e_time.pack(side="right")
self.exam_entries['time'] = e_time
ttk.Separator(scroll_content, orient='horizontal').pack(fill='x', pady=20)
# 预设管理 (放在底部,横向拉通)
tk.Label(scroll_content, text="预设管理:", font=fonts["ui"], bg="white", fg="#666").pack(anchor="w")
preset_frame = tk.Frame(scroll_content, bg="white")
preset_frame.pack(fill="x", pady=5)
self.combo_presets = ttk.Combobox(preset_frame, values=self.exam_cfg_mgr.get_names(), state="readonly", font=fonts["ui"])
self.combo_presets.pack(side="left", fill="x", expand=True, padx=(0, 10))
def load_preset(event=None):
name = self.combo_presets.get()
data = self.exam_cfg_mgr.get_preset(name)
if data:
for qt in ['single', 'multi', 'judge', 'fill']:
self.exam_entries[qt]['count'].delete(0, tk.END); self.exam_entries[qt]['count'].insert(0, data.get(f"{qt}_c", 0))
self.exam_entries[qt]['score'].delete(0, tk.END); self.exam_entries[qt]['score'].insert(0, data.get(f"{qt}_s", 1))
self.exam_entries['time'].delete(0, tk.END); self.exam_entries['time'].insert(0, data.get("time", 60))
self.combo_presets.bind("<<ComboboxSelected>>", load_preset)
# 保存预设按钮
def save_preset_btn():
name = simpledialog.askstring("保存配置", "请输入配置名称 (如: Config1):")
if name:
data = {}
try:
for qt in ['single', 'multi', 'judge', 'fill']:
data[f"{qt}_c"] = int(self.exam_entries[qt]['count'].get())
data[f"{qt}_s"] = float(self.exam_entries[qt]['score'].get())
data["time"] = int(self.exam_entries['time'].get())
self.exam_cfg_mgr.save_preset(name, data)
self.combo_presets['values'] = self.exam_cfg_mgr.get_names()
messagebox.showinfo("成功", "配置已保存")
except ValueError:
messagebox.showerror("错误", "请输入有效的数字")
tk.Button(preset_frame, text="保存为预设", command=save_preset_btn, bg="#e0e0e0", font=fonts["ui"]).pack(side="right")
# 开始考试
def start_exam_check():
final_qs = []
score_map = {}
total_score = 0
# 准备要保存的配置数据
current_settings = {}
try:
# 随机抽取题目
for qt in ['single', 'multi', 'judge', 'fill']:
req_c = int(self.exam_entries[qt]['count'].get())
req_s = float(self.exam_entries[qt]['score'].get())
avail = self.exam_entries[qt]['avail']
# 记录配置以便保存
current_settings[qt] = {'count': req_c, 'score': req_s}
if req_c > avail:
messagebox.showwarning("数量不足", f"{self.type_map[qt]} 请求 {req_c} 题,但仅有 {avail} 题!")
return
if req_c > 0:
pool = [q for q in all_qs if q.q_type == qt]
selected = random.sample(pool, req_c)
final_qs.extend(selected)
score_map[qt] = req_s
total_score += req_c * req_s
limit_min = int(self.exam_entries['time'].get())
current_settings['time'] = limit_min # 记录时间配置
if limit_min <= 0: return messagebox.showwarning("提示", "时间必须大于0")
if not final_qs: return messagebox.showwarning("提示", "未选择任何题目!")
# 保存当前配置到 config.json,下次自动加载
self.cfg.set("last_exam_settings", current_settings)
# 初始化考试数据
self.exam_mode = True
self.exam_score_map = score_map
self.exam_total_time = limit_min * 60
self.exam_start_timestamp = datetime.now()
self.exam_end_time = time.time() + self.exam_total_time
self.exam_answers = {}
self.current_repo_name = repo_dir
self.start_quiz(final_qs, shuffle=True)
except ValueError:
messagebox.showerror("错误", "请输入有效的数字!")
tk.Button(scroll_content, text="开始考试", command=start_exam_check,
bg="#4caf50", fg="white", font=("微软雅黑", 14, "bold"), height=2).pack(fill="x", pady=30)
def start_quiz(self, questions, shuffle):
self.current_queue = list(questions)
if shuffle:
random.shuffle(self.current_queue)
# 定义排序优先级:单选(0) -> 多选(1) -> 判断(2) -> 填空(3)
# 这保证了做题顺序和答题卡板块顺序一致
type_priority = {'single': 0, 'multi': 1, 'judge': 2, 'fill': 3}
# 执行排序
self.current_queue.sort(key=lambda q: type_priority.get(q.q_type, 99))
self.current_index = 0
self.session_stats = {'total': 0, 'correct': 0}
# 初始化答题卡状态
# key: 题目索引 (0, 1, 2...), value: True(对), False(错), None(未答)
self.quiz_results = {}
# 考试模式下,初始化答案记录
if self.exam_mode:
self.exam_answers = {} # 重置答案
self.update_exam_timer() # 启动计时器
self.show_question_ui()
# 倒计时
def update_exam_timer(self):
if not self.exam_mode: return
remaining = int(self.exam_end_time - time.time())
if remaining <= 0:
self.finish_exam(auto_submit=True)
return
# 更新UI显示 (需要 show_question_ui 创建了 timer_label 才能更新)
if hasattr(self, 'timer_label') and self.timer_label.winfo_exists():
m, s = divmod(remaining, 60)
color = "red" if remaining < 300 else "black" # 最后5分钟变红
self.timer_label.config(text=f"剩余 {m:02d}:{s:02d}", fg=color)
self.exam_timer_id = self.root.after(1000, self.update_exam_timer)
def finish_exam(self, auto_submit=False):
# 停止计时
if self.exam_timer_id:
self.root.after_cancel(self.exam_timer_id)
self.exam_timer_id = None
if not auto_submit:
if not messagebox.askyesno("交卷", "确定要交卷吗?\n交卷后将无法修改答案。"):
self.update_exam_timer() # 恢复计时
return
# 计算分数
my_score = 0
max_score = 0
correct_count = 0
details = [] # 用于以后可能的详细回顾
for i, q in enumerate(self.current_queue):
q_score = self.exam_score_map.get(q.q_type, 0)
max_score += q_score
user_ans = self.exam_answers.get(i, "")
is_correct = False
# 判分逻辑
if q.q_type in ['single', 'judge']:
is_correct = (user_ans == q.correct_answer)
elif q.q_type == 'multi':
# 这里的 user_ans 已经是排序后的字符串
correct_sorted = "".join(sorted(list(q.correct_answer)))
is_correct = (user_ans == correct_sorted)
elif q.q_type == 'fill':
is_correct = (user_ans == q.correct_answer)
if is_correct:
my_score += q_score
correct_count += 1
self.quiz_results[i] = is_correct # 用于答题卡显示红绿
# 计算耗时
used_time = int(self.exam_total_time - (self.exam_end_time - time.time()))
if used_time < 0: used_time = self.exam_total_time # 防止超时负数
# 保存记录
rec = {
"date": datetime.now().strftime("%Y-%m-%d %H:%M"),
"score": round(my_score, 1),
"total_score": round(max_score, 1),
"used_time": f"{used_time//60} 分 {used_time%60} 秒",
"correct_num": correct_count,
"total_num": len(self.current_queue)
}
self.exam_hist_mgr.add_record(self.current_repo_name, rec)
self.exam_mode = False # 退出考试模式
msg = f"考试结束!\n\n得分: {rec['score']} / {rec['total_score']}\n耗时: {rec['used_time']}\n正确率: {correct_count} / {len(self.current_queue)} = {correct_count / len(self.current_queue)}"
messagebox.showinfo("成绩单", msg)
self.setup_menu()