-
Notifications
You must be signed in to change notification settings - Fork 0
/
ryViewer2.py
738 lines (542 loc) · 24 KB
/
ryViewer2.py
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
#!/usr/bin/env python3
'''
ryViewer2.py
__main__viewer.py
中文龜程式 翻譯瀏覽器。
呂仁園 中文翻譯 修改。
2014/12/24, 2015/01/18, 01/23
'''
"""
----------------------------------------------
turtleDemo - Help
----------------------------------------------
This document has two sections:
(1) How to use the demo viewer
(2) How to add your own demos to the demo repository
(1) How to use the demo viewer.
Select a demoscript from the example menu.
The (syntax coloured) source code appears in the left
source code window. IT CANNOT BE EDITED, but ONLY VIEWED!
The demo viewer windows can be resized. The divider between text
and canvas can be moved by grabbing it with the mouse. The text font
size can be changed from the menu and with Control/Command '-'/'+'.
It can also be changed on most systems with Control-mousewheel
when the mouse is over the text.
Press START button to start the demo.
Stop execution by pressing the STOP button.
Clear screen by pressing the CLEAR button.
Restart by pressing the START button again.
SPECIAL demos, such as clock.py are those which run EVENTDRIVEN.
Press START button to start the demo.
- Until the EVENTLOOP is entered everything works
as in an ordinary demo script.
- When the EVENTLOOP is entered, you control the
application by using the mouse and/or keys (or it's
controlled by some timer events)
To stop it you can and must press the STOP button.
While the EVENTLOOP is running, the examples menu is disabled.
- Only after having pressed the STOP button, you may
restart it or choose another example script.
* * * * * * * *
In some rare situations there may occur interferences/conflicts
between events concerning the demo script and those concerning the
demo-viewer. (They run in the same process.) Strange behaviour may be
the consequence and in the worst case you must close and restart the
viewer.
* * * * * * * *
(2) How to add your own demos to the demo repository
- Place the file in the same directory as turtledemo/__main__.py
IMPORTANT! When imported, the demo should not modify the system
by calling functions in other modules, such as sys, tkinter, or
turtle. Global variables should be initialized in main().
- The code must contain a main() function which will
be executed by the viewer (see provided example scripts).
It may return a string which will be displayed in the Label below
the source code window (when execution has finished.)
- In order to run mydemo.py by itself, such as during development,
add the following at the end of the file:
if __name__ == '__main__':
main()
mainloop() # keep window open
python -m turtledemo.mydemo # will then run it
- If the demo is EVENT DRIVEN, main must return the string
"EVENTLOOP". This informs the demo viewer that the script is
still running and must be stopped by the user!
If an "EVENTLOOP" demo runs by itself, as with clock, which uses
ontimer, or minimal_hanoi, which loops by recursion, then the
code should catch the turtle.Terminator exception that will be
raised when the user presses the STOP button. (Paint is not such
a demo; it only acts in response to mouse clicks and movements.)
"""
import sys
import os
import random
from tkinter import *
from idlelib.Percolator import Percolator
from idlelib.ColorDelegator import ColorDelegator
from idlelib.textView import view_text
from turtledemo import __doc__ as about_turtledemo
#import turtle as turtleOld
import turtle_tc as turtle
#import turtle_jp as turtle # 勿貪心,淺嘗即可。免得失去焦點。
import time
import inspect
from ryTranslate import 翻譯一個檔案
try:
import ryGoogleTTS
except:
print('import ryGoogleTTS: fail, No TTS functions ')
pass
import io
import tokenize as tn
import importlib
def main():
demo= DemoWindow()
demo.root.mainloop()
#------------------------------------------------------
darwin= sys.platform == 'darwin' # 測試 是否在 macOS 上,有些特殊問題要解決(似乎比較麻煩)
STARTUP= 1
READY= 2
RUNNING= 3
DONE= 4
EVENTDRIVEN= 5
menufont= ("Arial", 12, NORMAL)
btnfont= ("Arial", 12, 'bold')
txtfont= ['Lucida Console', 12, 'normal']
MINIMUM_FONT_SIZE= 6
MAXIMUM_FONT_SIZE= 100
font_sizes= [8, 9, 10, 11, 12, 14, 18, 20, 22, 24, 30]
# 原本是拿 本程式所在地的 英文程式
demo_dir= os.path.dirname(os.path.abspath(__file__))
def getExampleEntries000():
return [entry[:-3] for entry in os.listdir(demo_dir) if
entry.endswith(".py") and entry[0] != '_']
# 我把它換成 直接拿官方的 turtledemo 來展示。
import turtledemo
turtledemoDir= os.path.dirname(os.path.abspath(turtledemo.__file__))
def getExampleEntries():
rootDir= turtledemoDir #demo_dir
for r, dL, fL in os.walk(rootDir):
if r not in sys.path:
sys.path += [r]
pyFiles = [f[:-3]
for r, dL, fL in os.walk(rootDir)
for f in fL if ( f.endswith('.py')
and (f[0]!='_')
)]
return pyFiles
help_entries = ( # (help_label, help_doc)
('Turtledemo help', __doc__),
('About turtledemo', about_turtledemo),
('About turtle module', turtle.__doc__),
('龜作圖 網上資源','https://docs.python.org/3.4/library/turtle.html')
)
class DemoWindow(object):
def __init__(我, filename=None):
#turtleOld._root=
我.root= root= turtle._root= Tk() #Toplevel() #Tk()
root.title('傳統漢字 (Traditional Chinese, tc) 龜作圖 範例集')
root.wm_protocol("WM_DELETE_WINDOW", 我._destroy)
if darwin:
import subprocess
# Make sure we are the currently activated OS X application
# so that our menu bar appears.
p = subprocess.Popen(
[
'osascript',
'-e', 'tell application "System Events"',
'-e', 'set frontmost of the first process whose '
'unix id is {} to true'.format(os.getpid()),
'-e', 'end tell',
],
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1, minsize=90)
root.grid_columnconfigure(2, weight=1, minsize=90)
root.grid_columnconfigure(3, weight=1, minsize=90)
我.mBar= Menu(root, relief=RAISED, borderwidth=2)
我.mBar.add_cascade(label='範例', menu= 我.makeLoadDemoMenu(我.mBar))#, underline=0)
我.mBar.add_cascade(label='字體', menu= 我.makeFontMenu(我.mBar))#, underline=0)
我.mBar.add_cascade(label='求助', menu= 我.makeHelpMenu(我.mBar))#, underline=0)
我.mBar.add_cascade(label='新功能', menu= 我.新功能選單(我.mBar))#, underline=0) # (1)
root['menu']= 我.mBar
pane= PanedWindow(orient=HORIZONTAL, bg='black')#'#ddd') #sashwidth=6, #sashrelief=SOLID,
pane.add(我.makeTextFrame(pane)) # for English program display
pane.add(我.makeTextFrame2(pane)) # for 漢字程式 展示 及修改
pane.add(我.makeGraphFrame(pane)) # for 畫圖
pane.grid(row=0, columnspan= 4, sticky='news')
我.output_lbl= Label(root, text="---", font=btnfont, bg="#ddf", borderwidth=2, relief=RIDGE)
我.start_btn= Button(root, text="開始", command= 我.startDemo, font=btnfont, fg="white", disabledforeground= "#fed")
我.stop_btn= Button(root, text="停止", command= 我.stopIt, font=btnfont, fg="white", disabledforeground= "#fed")
我.clear_btn= Button(root, text="清除", command= 我.clearCanvas, font=btnfont, fg="white", disabledforeground= "#fed")
我.output_lbl.grid(row=1, column=0, sticky='news', padx=(0,3))
我.start_btn.grid( row=1, column=1, sticky='ew')
我.stop_btn.grid( row=1, column=2, sticky='ew')
我.clear_btn.grid( row=1, column=3, sticky='ew')
# 讓 程式內文 有 語法 顏色,超酷的!
Percolator(我.text).insertfilter(ColorDelegator())
Percolator(我.text2).insertfilter(ColorDelegator())
我.dirty = False
我.exitflag = False
if filename:
我.loadfile(filename)
我.configGUI(DISABLED, DISABLED, DISABLED, "由選單 選範例", "black")
我.state = STARTUP
# 在 桌面上 建一個 _tcDir 來存放翻譯的檔案。
dektopDir= os.path.join(os.path.expanduser('~'), 'Desktop')
我.tcDir= tcDir= os.path.join(dektopDir, '_tcDir') ## 用底線代表用過可丟。
if not os.path.exists(tcDir):
os.mkdir(tcDir)
if tcDir not in sys.path: sys.path += [tcDir]
# 順便 把 turtle_tc.py 們 copy 過去,方便在裡面 修改 學習。
import shutil
for f in ['turtle_tc.py', 'turtle_tc_alias.py', 'turtle_docstringdict_tc.py']:
srcfile= os.path.join(os.path.dirname(__file__), f)
tgtfile= os.path.join(tcDir, f)
if not os.path.exists(tgtfile): shutil.copy(srcfile, tcDir)
def onResize(我, event):
cwidth = 我._canvas.winfo_width()
cheight = 我._canvas.winfo_height()
我._canvas.xview_moveto(0.5*(我.canvwidth-cwidth)/我.canvwidth)
我._canvas.yview_moveto(0.5*(我.canvheight-cheight)/我.canvheight)
def makeTextFrame(我, root):
我.text_frame= text_frame= Frame(root)
我.text= text= Text(text_frame, name='text', padx=5,
wrap='none', width=45, bg='#fafafa')
我.vbar= vbar= Scrollbar(text_frame, name='vbar')
vbar['command'] = text.yview
vbar.pack(side=LEFT, fill=Y)
我.hbar= hbar = Scrollbar(text_frame, name='hbar', orient=HORIZONTAL)
hbar['command'] = text.xview
hbar.pack(side=BOTTOM, fill=X)
text['yscrollcommand'] = vbar.set
text['xscrollcommand'] = hbar.set
text['font'] = tuple(txtfont)
shortcut = 'Command' if darwin else 'Control'
text.bind_all('<%s-minus>' % shortcut, 我.decrease_size)
text.bind_all('<%s-underscore>' % shortcut, 我.decrease_size)
text.bind_all('<%s-equal>' % shortcut, 我.increase_size)
text.bind_all('<%s-plus>' % shortcut, 我.increase_size)
text.bind('<Control-MouseWheel>', 我.update_mousewheel)
text.bind('<Control-Button-4>', 我.increase_size)
text.bind('<Control-Button-5>', 我.decrease_size)
text.pack(side=LEFT, fill=BOTH, expand=1)
return text_frame
def makeTextFrame2(我, root):
我.text_frame2= text_frame= Frame(root)
我.text2= text= Text(text_frame, name='text', padx=5,
wrap='none', width=45)
我.vbar2= vbar= Scrollbar(text_frame, name='vbar')
vbar['command'] = text.yview
vbar.pack(side=LEFT, fill=Y)
我.hbar2= hbar = Scrollbar(text_frame, name='hbar', orient=HORIZONTAL)
hbar['command'] = text.xview
hbar.pack(side=BOTTOM, fill=X)
text['yscrollcommand'] = vbar.set
text['xscrollcommand'] = hbar.set
text['font'] = tuple(txtfont)
shortcut = 'Command' if darwin else 'Control'
text.bind_all('<%s-minus>' % shortcut, 我.decrease_size)
text.bind_all('<%s-underscore>' % shortcut, 我.decrease_size)
text.bind_all('<%s-equal>' % shortcut, 我.increase_size)
text.bind_all('<%s-plus>' % shortcut, 我.increase_size)
text.bind('<Control-MouseWheel>', 我.update_mousewheel)
text.bind('<Control-Button-4>', 我.increase_size)
text.bind('<Control-Button-5>', 我.decrease_size)
text.pack(side=LEFT, fill=BOTH, expand=1)
return text_frame
def makeGraphFrame(我, root):
#turtleOld._Screen._root=
turtle._Screen._root= root
我.canvwidth = 1000
我.canvheight = 800
#turtleOld._Screen._canvas=
turtle._Screen._canvas= 我._canvas= canvas= turtle.ScrolledCanvas(
root, 800, 600, 我.canvwidth, 我.canvheight)
canvas.adjustScrolls()
canvas._rootwindow.bind('<Configure>', 我.onResize)
canvas._canvas['borderwidth'] = 0
我.screen = _s_ = turtle.Screen()
turtle.TurtleScreen.__init__(_s_, _s_._canvas)
我.scanvas = _s_._canvas
turtle.RawTurtle.screens = [_s_]
return canvas
def set_txtsize(我, size):
txtfont[1] = size
我.text['font'] = tuple(txtfont)
我.text2['font'] = tuple(txtfont)
我.output_lbl['text'] = '字體大小 %d' % size
def decrease_size(我, dummy=None):
我.set_txtsize(max(txtfont[1] - 1, MINIMUM_FONT_SIZE))
return 'break'
def increase_size(我, dummy=None):
我.set_txtsize(min(txtfont[1] + 1, MAXIMUM_FONT_SIZE))
return 'break'
def update_mousewheel(我, event):
# For wheel up, event.delte = 120 on Windows, -1 on darwin.
# X-11 sends Control-Button-4 event instead.
if (event.delta < 0) == (not darwin):
return 我.decrease_size()
else:
return 我.increase_size()
def configGUI(我, start, stop, clear, txt="", color="blue"):
我.start_btn.config(state=start,
bg="#d00" if start == NORMAL else "#fca")
我.stop_btn.config(state=stop,
bg="#d00" if stop == NORMAL else "#fca")
我.clear_btn.config(state=clear,
bg="#d00" if clear == NORMAL else"#fca")
我.output_lbl.config(text=txt, fg=color)
def makeLoadDemoMenu(我, master):
menu = Menu(master)
for 序號, entry in enumerate(getExampleEntries()):
#print('entry= ',entry) # 沒有 path 了
def load(entry=entry):
我.loadfile(entry)
menu.add_command(label='%02d. %s'%(序號, entry), underline=0,
font=menufont, command=load)
return menu
def 翻譯檔案(我):
fn= 我.module.__file__
#print('fn= ', fn)
fnBase= os.path.basename(fn)
if ('tc_' in fnBase) or ('td_' in fnBase):
fn= 我.module0.__file__
tcBase = 'tc_'+os.path.basename(fn)
# 在 桌面上 建一個 tcDir 來存放翻譯的檔案。
#dektopDir= os.path.join(os.path.expanduser('~'), 'Desktop')
#我.tcDir= tcDir= os.path.join(dektopDir, 'tcDir')
tcDir= 我.tcDir
tcPath= os.path.join(tcDir, tcBase)
tcSrc, enSrc= 翻譯一個檔案(fn, tcPath)
我.clearCanvas()
turtle.TurtleScreen._RUNNING = False
modname= tcBase.replace('.py','')
__import__(modname)
我.module = sys.modules[modname]
'''
with open(tcPath, 'r', encoding='utf8') as f:
chars = f.read()
'''
我.text2.delete("1.0", "end")
# -------------------------------------------
我.text2.insert("1.0", tcSrc) #enSrc) #chars)
我.root.title(fn + " --> [翻譯] --> " + tcPath )
我.configGUI(NORMAL, DISABLED, DISABLED,
"按 開始", "red")
我.state = READY
#return tcSrc
def 更新檔案(我):
fn= 我.module.__file__
#print('fn= ', fn)
tcBase= os.path.basename(fn)
tcBase= tcBase.replace('tc_','td_')
tcDir= 我.tcDir #='tcDir'
if not os.path.exists(tcDir):
os.mkdir(tcDir)
if tcDir not in sys.path: sys.path += [tcDir]
tcPath= os.path.join(tcDir, tcBase)
tcSrc= 我.text2.get('1.0', 'end')
with open(tcPath, 'w', encoding='utf-8') as fpnew:
fpnew.write(tcSrc)
我.clearCanvas()
turtle.TurtleScreen._RUNNING = False
modname= tcBase.replace('.py','')
__import__(modname)
我.module= sys.modules[modname]
我.module= importlib.reload(我.module)
我.root.title(fn + " 更新成 " + tcPath )
我.configGUI(NORMAL, DISABLED, DISABLED,
"按 開始", "red")
我.state = READY
def 開窗來跑(我):
'''
目前只准跑 翻譯後的 中文程式,
並讓人隨意修改之,
原版英文程式不准改,
因為那是系統的一部份。
本功能基本上可跑,
但在「關」窗時,
會出現一些 「髒」,
仍待改。
'''
import sys
'''
import idlelib
from idlelib import PyShell
PyShell= importlib.reload(idlelib.PyShell)
'''
#import idlelib.ryPyShell as PyShell
import idlelib.PyShell as PyShell
fn= 我.module.__file__
#print('fn= ', fn)
tcDir= 我.tcDir #= 'tcDir'
if os.path.abspath(os.path.dirname(fn)) != os.path.abspath(tcDir):
msg= '%s not in %s\n'%(fn, os.path.abspath(tcDir))
msg+= '目前只准跑 翻譯後的 中文程式,並隨意修改之。\n'
print(msg)
return
sys.argv= ['', '-r', fn]
#我.root.wm_protocol("WM_DELETE_WINDOW", 我._destroy)
PyShell.main() #### 這行 在 windows 上 OK; 但 mac 不 OK !!!!
# 因此 本功能 在 mac 上 仍有 bug
def 語音合成(我):
text= 我.text2.get('1.0', 'end')
fp= io.StringIO(text)
tokenL= [ x for x in tn.generate_tokens(fp.readline) ]
ryGoogleTTS.ttsIt(tokenL) #, langAnother= 'ja')
ryGoogleTTS.playIt()
def 語音合成之停止(我):
ryGoogleTTS.quitIt()
def 打亂程式(我):
tcSrc= 我.text2.get('sel.first', 'sel.last')
sL= tcSrc.split('\n')
random.shuffle(sL)
sT=''
for x in sL:
sT=sT+x+'\n'
我.text2.insert('sel.last',sT)
我.text2.delete('sel.first', 'sel.last')
我.text2.update()
def 音文同步(我):
if 'yinyang.py' not in 我.module.__file__:
msg= '非 yinyang.py, 暫無 音文同步!'
print(msg)
return msg
from rySyncTTS import RySyncTTS
x= RySyncTTS(我.text2)
x.rySync()
x.ryQuit()
#print('not implemented !!')
return
def 新功能選單(我, master):
menu= Menu(master)
menu.add_command(label= '翻譯(英->漢)', command= 我.翻譯檔案)
menu.add_separator()
menu.add_command(label= '更新(漢)', command= 我.更新檔案)
menu.add_separator()
menu.add_command(label= '開窗來跑(漢) mac 尚有問題!', command= 我.開窗來跑)
menu.add_separator()
menu.add_command(label= '語音合成(漢)', command= 我.語音合成)
menu.add_command(label= '語音合成之停止', command= 我.語音合成之停止)
menu.add_separator()
menu.add_command(label= '打亂程式(選到的部分)', command= 我.打亂程式)
menu.add_separator()
menu.add_command(label= '音文同步(tc_yinyang)', command= 我.音文同步)
return menu
def makeFontMenu(我, master):
menu = Menu(master)
menu.add_command(label="減 (C-'-')", command=我.decrease_size,
font=menufont)
menu.add_command(label="增 (C-'+')", command=我.increase_size,
font=menufont)
menu.add_separator()
for size in font_sizes:
def resize(size=size):
我.set_txtsize(size)
menu.add_command(label=str(size), underline=0,
font=menufont, command=resize)
return menu
def makeHelpMenu(我, master):
menu = Menu(master)
for help_label, help_file in help_entries:
def show(help_label=help_label, help_file=help_file):
if 'https:' in help_file:
try:
import webbrowser
webbrowser.open_new(help_file) #'https://docs.python.org/3.4/library/turtle.html'
except:
help_file += ' .. 但網路可能不通。'
view_text(我.root, help_label, help_file)
menu.add_command(label=help_label, font=menufont, command=show)
return menu
def refreshCanvas(我):
if 我.dirty:
我.screen.clear()
我.dirty=False
def loadfile(我, filename):
我.clearCanvas()
turtle.TurtleScreen._RUNNING = False
modname= filename #'turtledemo.' + filename
__import__(modname)
我.module = sys.modules[modname]
我.module0= 我.module # 保存起來,以備回復。
#
# 加入 encoding='utf8' 讓它可以讀中文程式
#
with open(我.module.__file__, 'r', encoding='utf8') as f:
chars = f.read()
我.text.configure(state='normal')
我.text.delete("1.0", "end")
#
# 以下加入行號,我假設程式在 9999 行以內。
#
# 將會影響 copy-paste 程式的便利性,
# 所以不必然好!可能先 mark 掉,
# 等能放在GUI中自由調控再說。
#
'''
aL= chars.split('\n')
X=''
for i,a in enumerate(aL):
x= '%04d%s%s\n'%(i,' '*4, a)
X += x
chars= X
'''
# -------------------------------------------
我.text.insert("1.0", chars)
#我.text.configure(state='disabled') # 這行會讓它「不能改,也不能copy-paste」
我.root.title(" Python 龜作圖 之 傳統漢字(Traditional Chinese, tc) 範例。" + filename )
我.configGUI(NORMAL, DISABLED, DISABLED,
"按 開始", "red")
我.state = READY
我.翻譯檔案() # 一輸入完畢 loadfile() 就直接翻譯了。
def startDemo(我):
我.更新檔案() # 一按「開始」就先更新了。
我.refreshCanvas()
我.dirty = True
turtle.TurtleScreen._RUNNING = True
我.configGUI(DISABLED, NORMAL, DISABLED,
"執行中...", "black")
我.screen.clear()
我.screen.mode("standard")
我.state = RUNNING
try:
if 'main' in vars(我.module):
result = 我.module.main()
elif '主函數' in vars(我.module):
result = 我.module.主函數()
else:
result = ''
if result == "EVENTLOOP":
我.state = EVENTDRIVEN
else:
我.state = DONE
except turtle.Terminator:
我.state = DONE
result = "已停止!"
if 我.state == DONE:
我.configGUI(NORMAL, DISABLED, NORMAL,
result)
elif 我.state == EVENTDRIVEN:
我.exitflag = True
我.configGUI(DISABLED, NORMAL, DISABLED,
"用滑鼠、鍵盤 控制螢幕 或 按 停止", "red")
def clearCanvas(我):
我.refreshCanvas()
我.screen._delete("all")
我.scanvas.config(cursor="")
我.configGUI(NORMAL, DISABLED, DISABLED)
def stopIt(我):
if 我.exitflag:
我.clearCanvas()
我.exitflag = False
我.configGUI(NORMAL, DISABLED, DISABLED,
"已停止!", "red")
turtle.TurtleScreen._RUNNING = False
def _destroy(我):
我.root.destroy()
if __name__ == '__main__':
main()