-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchinese_python_gui.py
More file actions
1110 lines (891 loc) · 43.2 KB
/
chinese_python_gui.py
File metadata and controls
1110 lines (891 loc) · 43.2 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 更新日志
"""
更新日志:
2023-09-09: 初始版本发布
2024-05-18:
- 修复正则表达式转义序列错误
- 移除30秒执行时间限制
- 增加实时输出功能
- 增加停止执行功能
- 改进UI交互体验
"""
import os
import sys
import tkinter as tk
from tkinter import ttk, scrolledtext, filedialog, messagebox
import subprocess
from pathlib import Path
import re
import threading
import time
# 导入中文Python解释器模块
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import chinese_python
# 从mapping模块导入翻译函数
import mapping
class PythonHighlighter:
"""Python代码语法高亮器"""
def __init__(self, text_widget):
self.text_widget = text_widget
# 配置标签
self.text_widget.tag_configure("keyword", foreground="#0000FF") # 关键字:蓝色
self.text_widget.tag_configure("string", foreground="#008000") # 字符串:绿色
self.text_widget.tag_configure("comment", foreground="#808080") # 注释:灰色
self.text_widget.tag_configure("function", foreground="#800080") # 函数:紫色
self.text_widget.tag_configure("number", foreground="#FF8000") # 数字:橙色
self.text_widget.tag_configure("decorator", foreground="#CC0000")# 装饰器:红色
# Python关键字列表
self.keywords = [
"False", "None", "True", "and", "as", "assert", "async", "await", "break",
"class", "continue", "def", "del", "elif", "else", "except", "finally",
"for", "from", "global", "if", "import", "in", "is", "lambda", "nonlocal",
"not", "or", "pass", "raise", "return", "try", "while", "with", "yield"
]
# 内置函数列表
self.builtins = [
"abs", "all", "any", "bin", "bool", "bytearray", "bytes", "callable", "chr",
"classmethod", "compile", "complex", "delattr", "dict", "dir", "divmod", "enumerate",
"eval", "exec", "filter", "float", "format", "frozenset", "getattr", "globals",
"hasattr", "hash", "help", "hex", "id", "input", "int", "isinstance", "issubclass",
"iter", "len", "list", "locals", "map", "max", "memoryview", "min", "next", "object",
"oct", "open", "ord", "pow", "print", "property", "range", "repr", "reversed", "round",
"set", "setattr", "slice", "sorted", "staticmethod", "str", "sum", "super", "tuple",
"type", "vars", "zip", "__import__"
]
def highlight_code(self, code):
"""对代码进行语法高亮"""
# 清除所有现有标签
for tag in ["keyword", "string", "comment", "function", "number", "decorator"]:
self.text_widget.tag_remove(tag, "1.0", tk.END)
# 获取文本内容
content = self.text_widget.get("1.0", tk.END)
# 简单的行处理方式
lines = content.split('\n')
for i, line in enumerate(lines):
line_num = i + 1
# 高亮注释
if '#' in line:
comment_start = line.find('#')
self.text_widget.tag_add("comment", f"{line_num}.{comment_start}", f"{line_num}.end")
# 高亮关键字
for keyword in self.keywords:
start = 0
while True:
start = line.find(keyword, start)
if start == -1:
break
# 确保是独立的单词
end = start + len(keyword)
if (start == 0 or not line[start-1].isalnum()) and (end >= len(line) or not line[end].isalnum()):
self.text_widget.tag_add("keyword", f"{line_num}.{start}", f"{line_num}.{end}")
start = end
# 高亮内置函数
for func in self.builtins:
start = 0
while True:
start = line.find(func, start)
if start == -1:
break
# 确保是独立的单词
end = start + len(func)
if (start == 0 or not line[start-1].isalnum()) and (end >= len(line) or not line[end].isalnum()):
self.text_widget.tag_add("function", f"{line_num}.{start}", f"{line_num}.{end}")
start = end
# 高亮字符串
self._highlight_strings_in_line(line, line_num)
def _highlight_pattern(self, pattern, tag):
"""使用正则表达式查找并应用标签"""
# 设置文本小部件为正常状态以进行编辑
current_state = self.text_widget.cget("state")
self.text_widget.config(state="normal")
try:
start = "1.0"
while True:
try:
# 查找模式
pos = self.text_widget.search(pattern, start, tk.END, regexp=True)
if not pos:
break
# 计算匹配的结束位置
line, col = pos.split('.')
line, col = int(line), int(col)
# 获取匹配的文本
line_text = self.text_widget.get(f"{line}.0", f"{line}.end")
try:
match = re.search(pattern, line_text[col:])
if not match:
start = f"{line+1}.0"
continue
match_length = len(match.group(0))
end = f"{line}.{col + match_length}"
# 应用标签
self.text_widget.tag_add(tag, pos, end)
# 移动到下一个位置
start = end
except re.error:
# 如果正则表达式搜索出错,跳过当前行
start = f"{line+1}.0"
except Exception as e:
# 如果处理某一匹配项出错,继续处理下一个
print(f"高亮错误: {e}")
break
except Exception as e:
# 如果整个高亮过程出错,打印错误但不中断程序
print(f"语法高亮错误: {e}")
# 恢复文本小部件的原始状态
self.text_widget.config(state=current_state)
def _highlight_strings_in_line(self, line, line_num):
"""在单行中高亮字符串"""
# 高亮双引号字符串
in_string = False
start_pos = -1
i = 0
while i < len(line):
if line[i] == '"' and (i == 0 or line[i-1] != '\\'):
if not in_string: # 字符串开始
in_string = True
start_pos = i
else: # 字符串结束
in_string = False
self.text_widget.tag_add("string", f"{line_num}.{start_pos}", f"{line_num}.{i+1}")
i += 1
# 高亮单引号字符串
in_string = False
start_pos = -1
i = 0
while i < len(line):
if line[i] == "'" and (i == 0 or line[i-1] != '\\'):
if not in_string: # 字符串开始
in_string = True
start_pos = i
else: # 字符串结束
in_string = False
self.text_widget.tag_add("string", f"{line_num}.{start_pos}", f"{line_num}.{i+1}")
i += 1
class ChinesePythonGUI:
"""
中文Python解释器的图形用户界面
"""
def __init__(self, root):
self.root = root
self.root.title("中文Python编辑器")
self.root.geometry("1000x700")
# 设置图标和主题
self.setup_appearance()
# 当前打开的文件路径
self.current_file = None
# 创建线程控制变量
self.stop_execution = threading.Event()
self.execution_thread = None
# 创建界面组件
self.create_widgets()
# 绑定快捷键
self.bind_shortcuts()
def setup_appearance(self):
"""设置界面外观"""
# 尝试设置一个更好看的主题
try:
self.root.tk.call("source", "azure.tcl")
self.root.tk.call("set_theme", "light")
except tk.TclError:
# 如果没有azure主题,使用默认主题
pass
# 设置窗口图标
try:
self.root.iconbitmap("python_icon.ico")
except tk.TclError:
# 如果没有图标文件,忽略
pass
def create_widgets(self):
"""创建界面组件"""
# 创建主框架
self.main_frame = ttk.Frame(self.root)
self.main_frame.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
# 创建菜单栏
self.create_menu()
# 创建工具栏
self.create_toolbar()
# 创建编辑器区域
self.create_editor_area()
# 创建输出区域
self.create_output_area()
# 创建状态栏
self.create_status_bar()
def create_menu(self):
"""创建菜单栏"""
self.menu_bar = tk.Menu(self.root)
# 文件菜单
self.file_menu = tk.Menu(self.menu_bar, tearoff=0)
self.file_menu.add_command(label="新建", command=self.new_file, accelerator="Ctrl+N")
self.file_menu.add_command(label="打开", command=self.open_file, accelerator="Ctrl+O")
self.file_menu.add_command(label="保存", command=self.save_file, accelerator="Ctrl+S")
self.file_menu.add_command(label="另存为", command=self.save_file_as, accelerator="Ctrl+Shift+S")
self.file_menu.add_separator()
self.file_menu.add_command(label="退出", command=self.exit_app, accelerator="Alt+F4")
self.menu_bar.add_cascade(label="文件", menu=self.file_menu)
# 编辑菜单
self.edit_menu = tk.Menu(self.menu_bar, tearoff=0)
self.edit_menu.add_command(label="撤销", command=lambda: self.editor.event_generate("<<Undo>>"), accelerator="Ctrl+Z")
self.edit_menu.add_command(label="重做", command=lambda: self.editor.event_generate("<<Redo>>"), accelerator="Ctrl+Y")
self.edit_menu.add_separator()
self.edit_menu.add_command(label="剪切", command=lambda: self.editor.event_generate("<<Cut>>"), accelerator="Ctrl+X")
self.edit_menu.add_command(label="复制", command=lambda: self.editor.event_generate("<<Copy>>"), accelerator="Ctrl+C")
self.edit_menu.add_command(label="粘贴", command=lambda: self.editor.event_generate("<<Paste>>"), accelerator="Ctrl+V")
self.menu_bar.add_cascade(label="编辑", menu=self.edit_menu)
# 运行菜单
self.run_menu = tk.Menu(self.menu_bar, tearoff=0)
self.run_menu.add_command(label="运行", command=self.run_code, accelerator="F5")
self.run_menu.add_command(label="停止", command=self.stop_code, accelerator="F12")
self.run_menu.add_command(label="清空输出", command=self.clear_output, accelerator="F6")
self.menu_bar.add_cascade(label="运行", menu=self.run_menu)
# 帮助菜单
self.help_menu = tk.Menu(self.menu_bar, tearoff=0)
self.help_menu.add_command(label="关于", command=self.show_about)
self.help_menu.add_command(label="帮助", command=self.show_help)
self.menu_bar.add_cascade(label="帮助", menu=self.help_menu)
# 设置菜单栏
self.root.config(menu=self.menu_bar)
def create_toolbar(self):
"""创建工具栏"""
self.toolbar = ttk.Frame(self.main_frame)
self.toolbar.pack(side=tk.TOP, fill=tk.X)
# 新建按钮
self.new_button = ttk.Button(self.toolbar, text="新建", command=self.new_file)
self.new_button.pack(side=tk.LEFT, padx=2, pady=2)
# 打开按钮
self.open_button = ttk.Button(self.toolbar, text="打开", command=self.open_file)
self.open_button.pack(side=tk.LEFT, padx=2, pady=2)
# 保存按钮
self.save_button = ttk.Button(self.toolbar, text="保存", command=self.save_file)
self.save_button.pack(side=tk.LEFT, padx=2, pady=2)
# 运行按钮
self.run_button = ttk.Button(self.toolbar, text="运行", command=self.run_code)
self.run_button.pack(side=tk.LEFT, padx=2, pady=2)
# 停止按钮
self.stop_button = ttk.Button(self.toolbar, text="停止", command=self.stop_code, state="disabled")
self.stop_button.pack(side=tk.LEFT, padx=2, pady=2)
# 清空输出按钮
self.clear_button = ttk.Button(self.toolbar, text="清空输出", command=self.clear_output)
self.clear_button.pack(side=tk.LEFT, padx=2, pady=2)
def create_editor_area(self):
"""创建编辑器区域"""
# 创建编辑器框架
self.editor_frame = ttk.LabelFrame(self.main_frame, text="代码编辑器")
self.editor_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True, padx=5, pady=5)
# 创建编辑器
self.editor = scrolledtext.ScrolledText(
self.editor_frame,
wrap=tk.WORD,
width=80,
height=20,
font=("Consolas", 12)
)
self.editor.pack(fill=tk.BOTH, expand=True)
# 设置编辑器的Tab行为
self.editor.bind("<Tab>", self.handle_tab)
# 设置语法高亮(简单实现)
self.setup_syntax_highlighting()
def create_output_area(self):
"""创建输出区域"""
# 创建输出框架
self.output_container = ttk.Frame(self.main_frame)
self.output_container.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True, padx=5, pady=5)
# 创建转换后代码框架
self.converted_frame = ttk.LabelFrame(self.output_container, text="转换后的Python代码")
self.converted_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=2, pady=5)
# 创建转换后代码文本框
self.converted_code = scrolledtext.ScrolledText(
self.converted_frame,
wrap=tk.WORD,
width=40,
height=10,
font=("Consolas", 10),
bg="#f8f8f8",
state="disabled" # 初始状态为禁用,防止用户编辑
)
self.converted_code.pack(fill=tk.BOTH, expand=True)
# 为转换后的Python代码添加语法高亮
self.python_highlighter = PythonHighlighter(self.converted_code)
# 创建执行结果框架
self.output_frame = ttk.LabelFrame(self.output_container, text="执行结果")
self.output_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True, padx=2, pady=5)
# 创建执行结果文本框
self.output = scrolledtext.ScrolledText(
self.output_frame,
wrap=tk.WORD,
width=40,
height=10,
font=("Consolas", 10),
bg="#f0f0f0",
state="disabled" # 初始状态为禁用,防止用户编辑
)
self.output.pack(fill=tk.BOTH, expand=True)
# 配置输出区域的文本标签
self.output.tag_configure("normal", foreground="#000000") # 普通输出:黑色
self.output.tag_configure("error", foreground="#FF0000") # 错误输出:红色
self.output.tag_configure("warning", foreground="#FFA500") # 警告输出:橙色
self.output.tag_configure("success", foreground="#008000") # 成功输出:绿色
self.output.tag_configure("info", foreground="#0000FF") # 信息输出:蓝色
def create_status_bar(self):
"""创建状态栏"""
self.status_bar = ttk.Label(self.root, text="就绪", anchor=tk.W)
self.status_bar.pack(side=tk.BOTTOM, fill=tk.X)
def setup_syntax_highlighting(self):
"""设置简单的语法高亮"""
# 这里只是一个简单的实现,实际上需要更复杂的逻辑
# 可以考虑使用第三方库如pygments来实现完整的语法高亮
self.editor.tag_configure("keyword", foreground="blue")
self.editor.tag_configure("string", foreground="green")
self.editor.tag_configure("comment", foreground="gray")
# 绑定按键事件以触发语法高亮
self.editor.bind("<KeyRelease>", self.highlight_syntax)
def bind_shortcuts(self):
"""绑定快捷键"""
# 文件操作快捷键
self.root.bind("<Control-n>", lambda e: self.new_file())
self.root.bind("<Control-o>", lambda e: self.open_file())
self.root.bind("<Control-s>", lambda e: self.save_file())
self.root.bind("<Control-Shift-S>", lambda e: self.save_file_as())
# 运行快捷键
self.root.bind("<F5>", lambda e: self.run_code())
self.root.bind("<F6>", lambda e: self.clear_output())
self.root.bind("<F12>", lambda e: self.stop_code())
# Tab键处理
self.editor.bind("<Tab>", self.handle_tab)
def handle_tab(self, event):
"""处理Tab键,插入4个空格而不是制表符"""
# 插入4个空格
self.editor.insert(tk.INSERT, " ")
# 阻止默认的Tab行为
return "break"
def new_file(self):
"""创建新文件"""
# 如果当前文件已修改,提示保存
if self.is_modified():
if not self.confirm_save():
return # 用户取消了操作
# 清空编辑器
self.editor.delete(1.0, tk.END)
# 重置当前文件路径
self.current_file = None
# 更新状态栏
self.update_status("新建文件")
def open_file(self):
"""打开文件"""
# 如果当前文件已修改,提示保存
if self.is_modified():
if not self.confirm_save():
return # 用户取消了操作
# 打开文件对话框
file_path = filedialog.askopenfilename(
defaultextension=".cnpy",
filetypes=[("中文Python文件", "*.cnpy"), ("所有文件", "*.*")]
)
if file_path:
try:
# 读取文件内容
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
# 清空编辑器并插入文件内容
self.editor.delete(1.0, tk.END)
self.editor.insert(tk.END, content)
# 更新当前文件路径
self.current_file = file_path
# 更新状态栏
self.update_status(f"已打开: {file_path}")
# 触发语法高亮
self.highlight_syntax(None)
except Exception as e:
messagebox.showerror("错误", f"无法打开文件: {e}")
def save_file(self):
"""保存文件"""
if self.current_file:
try:
# 获取编辑器内容
content = self.editor.get(1.0, tk.END)
# 保存到文件
with open(self.current_file, "w", encoding="utf-8") as file:
file.write(content)
# 更新状态栏
self.update_status(f"已保存: {self.current_file}")
return True
except Exception as e:
messagebox.showerror("错误", f"无法保存文件: {e}")
return False
else:
# 如果没有当前文件路径,执行另存为操作
return self.save_file_as()
def save_file_as(self):
"""另存为文件"""
# 打开保存文件对话框
file_path = filedialog.asksaveasfilename(
defaultextension=".cnpy",
filetypes=[("中文Python文件", "*.cnpy"), ("所有文件", "*.*")]
)
if file_path:
# 更新当前文件路径
self.current_file = file_path
# 执行保存操作
return self.save_file()
return False
def is_modified(self):
"""检查编辑器内容是否已修改"""
# 这里可以实现更复杂的逻辑,例如与原始文件内容比较
# 简单起见,我们假设任何非空内容都表示已修改
return len(self.editor.get(1.0, tk.END).strip()) > 0
def confirm_save(self):
"""确认是否保存修改"""
response = messagebox.askyesnocancel("保存", "是否保存当前更改?")
if response is None: # 取消操作
return False
elif response: # 是,保存
return self.save_file()
else: # 否,不保存
return True
def exit_app(self):
"""退出应用"""
# 如果当前文件已修改,提示保存
if self.is_modified():
if not self.confirm_save():
return # 用户取消了操作
# 退出应用
self.root.quit()
def run_code(self):
"""运行代码"""
# 获取编辑器内容
code = self.editor.get(1.0, tk.END)
# 如果代码为空,不执行
if not code.strip():
self.update_status("没有代码可执行")
return
# 如果当前文件已修改,提示保存
if self.current_file and self.is_modified():
if messagebox.askyesno("保存", "运行前是否保存当前更改?"):
if not self.save_file():
return # 保存失败,不执行
# 清空输出区域
self.clear_output()
# 更新状态栏
self.update_status("正在执行...")
# 重置停止标志
self.stop_execution.clear()
# 启用停止按钮
self.stop_button.config(state="normal")
try:
# 如果有保存的文件,直接运行该文件
if self.current_file:
self.run_file(self.current_file)
else:
# 否则,直接执行代码而不保存为文件
self.execute_code_directly(code)
self.update_status("执行完成")
except Exception as e:
self.append_output(f"执行错误: {e}\n", "error")
self.update_status("执行出错")
# 执行完毕后禁用停止按钮
self.stop_button.config(state="disabled")
def execute_code_directly(self, code):
"""直接执行中文Python代码"""
try:
# 转换中文代码到Python代码
python_code = mapping.translate_to_python(code)
# 显示转换后的代码到转换代码框
self.set_converted_code(python_code)
# 清空输出区域
self.clear_output()
# 显示执行开始信息
self.append_output("开始执行代码...\n", "info")
# 使用线程执行长时间运行的代码
self.run_with_timeout(python_code)
except Exception as e:
self.append_output(f"执行错误: {e}\n", "error")
def run_with_timeout(self, python_code, timeout=3600):
"""使用线程执行代码
参数:
python_code: 要执行的Python代码
timeout: 超时时间(秒), 默认3600秒(1小时)
"""
# 创建自定义输出流,实现实时输出到GUI
class OutputRedirector:
def __init__(self, text_widget, tag="normal"):
self.text_widget = text_widget
self.tag = tag
self.buffer = ""
def write(self, string):
self.buffer += string
if "\n" in self.buffer or len(self.buffer) > 80: # 当有换行或缓冲区超过80字符时更新GUI
self.text_widget.config(state="normal")
self.text_widget.insert(tk.END, self.buffer, self.tag)
self.text_widget.see(tk.END)
self.text_widget.config(state="disabled")
self.text_widget.update() # 强制更新GUI
self.buffer = ""
def flush(self):
if self.buffer:
self.text_widget.config(state="normal")
self.text_widget.insert(tk.END, self.buffer, self.tag)
self.text_widget.see(tk.END)
self.text_widget.config(state="disabled")
self.text_widget.update()
self.buffer = ""
# 重定向标准输出和标准错误
original_stdout = sys.stdout
original_stderr = sys.stderr
# 创建自定义输出重定向器
stdout_redirector = OutputRedirector(self.output, "normal")
stderr_redirector = OutputRedirector(self.output, "error")
# 创建事件用于通知执行完成
execution_done = threading.Event()
execution_result = {"success": False}
# 定义执行线程
def execution_thread():
sys.stdout = stdout_redirector
sys.stderr = stderr_redirector
try:
# 创建命名空间
namespace = {
'sys': sys,
'__builtins__': __builtins__,
# 确保关键词映射也可用
'类型': type,
}
exec(python_code, namespace)
execution_result["success"] = True
except Exception as e:
stderr_redirector.write(str(e))
finally:
# 确保缓冲区内容被刷新
stdout_redirector.flush()
stderr_redirector.flush()
# 恢复标准输出和标准错误
sys.stdout = original_stdout
sys.stderr = original_stderr
# 通知执行完成
execution_done.set()
# 创建并启动执行线程
self.execution_thread = threading.Thread(target=execution_thread)
self.execution_thread.daemon = True # 设置为守护线程,这样主程序退出时线程也会退出
self.execution_thread.start()
# 在UI上显示执行中的提示
self.update_status("正在执行代码...")
self.root.update()
# 等待执行完成或超时
start_time = time.time()
# 创建进度动画字符
progress_chars = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
char_index = 0
# 实时显示进度
self.append_output("正在执行代码...\n", "info")
progress_line = self.output.index("end-1c linestart")
while not execution_done.is_set() and (time.time() - start_time) < timeout:
# 检查是否要求停止
if self.stop_execution.is_set():
self.append_output("\n执行已被用户停止!\n", "warning")
self.update_status("执行已停止")
return
# 更新进度动画
elapsed = time.time() - start_time
if elapsed % 0.2 < 0.1: # 每0.2秒更新一次动画
char_index = (char_index + 1) % len(progress_chars)
# 更新状态栏
self.update_status(f"正在执行代码... {progress_chars[char_index]} (已用时 {elapsed:.1f} 秒)")
# 更新UI
self.root.update()
time.sleep(0.05)
# 检查是否超时
if not execution_done.is_set():
self.output.config(state="normal")
self.output.delete(progress_line, "end-1c")
self.output.config(state="disabled")
self.append_output("\n⚠️ 代码执行超时!", "warning")
self.append_output("\n可能原因:\n", "warning")
self.append_output("1. 代码中存在无限循环\n", "warning")
self.append_output("2. 计算量过大,需要更长时间\n", "warning")
self.append_output("3. 存在等待用户输入的代码\n", "warning")
self.update_status("代码执行超时")
return
# 显示执行结果
if execution_result["success"]:
self.update_status("代码执行完成")
self.append_output("\n代码执行成功!\n", "success")
else:
self.update_status("代码执行出错")
def run_file(self, file_path):
"""运行指定的文件"""
try:
# 读取文件内容
with open(file_path, "r", encoding="utf-8") as f:
code = f.read()
# 转换中文代码到Python代码
python_code = mapping.translate_to_python(code)
# 显示转换后的代码到转换代码框
self.set_converted_code(python_code)
# 清空输出区域
self.clear_output()
# 显示执行开始信息
self.append_output(f"开始执行文件: {os.path.basename(file_path)}\n", "info")
# 使用超时机制执行文件
self.run_file_with_timeout(file_path)
except Exception as e:
self.append_output(f"运行文件时出错: {e}\n", "error")
self.update_status("运行文件出错")
def run_file_with_timeout(self, file_path, timeout=3600):
"""使用线程执行文件
参数:
file_path: 要执行的文件路径
timeout: 超时时间(秒), 默认3600秒(1小时)
"""
# 创建自定义输出流,实现实时输出到GUI
class OutputRedirector:
def __init__(self, text_widget, tag="normal"):
self.text_widget = text_widget
self.tag = tag
self.buffer = ""
def write(self, string):
self.buffer += string
if "\n" in self.buffer or len(self.buffer) > 80: # 当有换行或缓冲区超过80字符时更新GUI
self.text_widget.config(state="normal")
self.text_widget.insert(tk.END, self.buffer, self.tag)
self.text_widget.see(tk.END)
self.text_widget.config(state="disabled")
self.text_widget.update() # 强制更新GUI
self.buffer = ""
def flush(self):
if self.buffer:
self.text_widget.config(state="normal")
self.text_widget.insert(tk.END, self.buffer, self.tag)
self.text_widget.see(tk.END)
self.text_widget.config(state="disabled")
self.text_widget.update()
self.buffer = ""
# 重定向标准输出和标准错误
original_stdout = sys.stdout
original_stderr = sys.stderr
# 创建自定义输出重定向器
stdout_redirector = OutputRedirector(self.output, "normal")
stderr_redirector = OutputRedirector(self.output, "error")
# 创建事件用于通知执行完成
execution_done = threading.Event()
execution_result = {"success": False}
# 定义执行线程
def execution_thread():
sys.stdout = stdout_redirector
sys.stderr = stderr_redirector
try:
# 直接调用chinese_python模块的函数运行文件
chinese_python.run_cnpy_file(str(file_path))
execution_result["success"] = True
except Exception as e:
stderr_redirector.write(str(e))
finally:
# 确保缓冲区内容被刷新
stdout_redirector.flush()
stderr_redirector.flush()
# 恢复标准输出和标准错误
sys.stdout = original_stdout
sys.stderr = original_stderr
# 通知执行完成
execution_done.set()
# 创建并启动执行线程
self.execution_thread = threading.Thread(target=execution_thread)
self.execution_thread.daemon = True # 设置为守护线程,这样主程序退出时线程也会退出
self.execution_thread.start()
# 在UI上显示执行中的提示
self.update_status("正在执行文件...")
self.root.update()
# 等待执行完成或超时
start_time = time.time()
# 创建进度动画字符
progress_chars = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
char_index = 0
# 实时显示进度
self.append_output(f"正在执行文件: {os.path.basename(file_path)}...\n", "info")
progress_line = self.output.index("end-1c linestart")
while not execution_done.is_set() and (time.time() - start_time) < timeout:
# 检查是否要求停止
if self.stop_execution.is_set():
self.append_output("\n执行已被用户停止!\n", "warning")
self.update_status("执行已停止")
return
# 更新进度动画
elapsed = time.time() - start_time
if elapsed % 0.2 < 0.1: # 每0.2秒更新一次动画
char_index = (char_index + 1) % len(progress_chars)
# 更新状态栏
self.update_status(f"正在执行文件... {progress_chars[char_index]} (已用时 {elapsed:.1f} 秒)")
# 更新UI
self.root.update()
time.sleep(0.05)
# 检查是否超时
if not execution_done.is_set():
self.output.config(state="normal")
self.output.delete(progress_line, "end-1c")
self.output.config(state="disabled")
self.append_output("\n⚠️ 文件执行超时!", "warning")
self.append_output("\n可能原因:\n", "warning")
self.append_output("1. 代码中存在无限循环\n", "warning")
self.append_output("2. 计算量过大,需要更长时间\n", "warning")
self.append_output("3. 存在等待用户输入的代码\n", "warning")
self.update_status("文件执行超时")
return
# 显示执行结果
if execution_result["success"]:
self.update_status("文件执行完成")
self.append_output("\n文件执行成功!\n", "success")
else:
self.update_status("文件执行出错")
def clear_output(self):
"""清空输出区域"""
# 清空转换后代码框
self.converted_code.config(state="normal")
self.converted_code.delete(1.0, tk.END)
self.converted_code.config(state="disabled")
# 清空执行结果框
self.output.config(state="normal")
self.output.delete(1.0, tk.END)
self.output.config(state="disabled")
def append_output(self, text, tag="normal"):
"""向输出区域添加文本
参数:
text: 要添加的文本
tag: 文本标签,可以是 "normal", "error", "warning", "success", "info"
"""
self.output.config(state="normal")
self.output.insert(tk.END, text, tag)
self.output.see(tk.END) # 滚动到底部
self.output.config(state="disabled")
def set_converted_code(self, code):
"""设置转换后的Python代码"""
self.converted_code.config(state="normal")
self.converted_code.delete(1.0, tk.END)
# 处理长代码显示
if len(code) > 10000: # 如果代码超过10000个字符
# 只显示前5000个字符和后5000个字符
truncated_code = code[:5000] + "\n\n...... 代码过长,中间部分已省略 ......\n\n" + code[-5000:]
self.converted_code.insert(tk.END, truncated_code)
self.append_output("注意:代码过长,转换后代码框中只显示部分内容。\n", "warning")
else:
self.converted_code.insert(tk.END, code)
# 应用语法高亮
self.python_highlighter.highlight_code(self.converted_code.get(1.0, tk.END))
self.converted_code.see(1.0) # 滚动到顶部
self.converted_code.config(state="disabled")
def update_status(self, message):
"""更新状态栏消息"""
self.status_bar.config(text=message)
def highlight_syntax(self, event=None):
"""简单的语法高亮实现"""
# 先移除所有标签
for tag in ["keyword", "string", "comment"]:
self.editor.tag_remove(tag, "1.0", tk.END)
# 获取文本内容
content = self.editor.get("1.0", tk.END)
# 关键字列表
keywords = [
"打印", "如果", "否则", "否则如果", "对于", "在", "当", "尝试", "捕获", "最后",
"定义", "类", "返回", "跳过", "中断", "继续", "导入", "从", "作为", "真", "假", "空"
]
# 高亮关键字
for keyword in keywords:
start_index = "1.0"
while True:
start_index = self.editor.search(keyword, start_index, stopindex=tk.END)
if not start_index:
break
end_index = f"{start_index}+{len(keyword)}c"
self.editor.tag_add("keyword", start_index, end_index)
start_index = end_index
# 高亮字符串 (简单实现,不处理转义)
start_index = "1.0"
while True:
start_index = self.editor.search('"', start_index, stopindex=tk.END)
if not start_index:
break
end_index = self.editor.search('"', f"{start_index}+1c", stopindex=tk.END)