Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f51531e

Browse files
committed
Issue #21823: Catch turtle.Terminator exceptions in turtledemo.
Add note to demohelp.txt about doing so.
1 parent fabefc3 commit f51531e

3 files changed

Lines changed: 37 additions & 25 deletions

File tree

Lib/turtledemo/clock.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
------------------------------------
1212
"""
1313
from turtle import *
14+
from turtle import Terminator # not in __all__
1415
from datetime import datetime
1516

1617
mode("logo")
@@ -102,22 +103,25 @@ def tick():
102103
sekunde = t.second + t.microsecond*0.000001
103104
minute = t.minute + sekunde/60.0
104105
stunde = t.hour + minute/60.0
105-
tracer(False)
106-
writer.clear()
107-
writer.home()
108-
writer.forward(65)
109-
writer.write(wochentag(t),
110-
align="center", font=("Courier", 14, "bold"))
111-
writer.back(150)
112-
writer.write(datum(t),
113-
align="center", font=("Courier", 14, "bold"))
114-
writer.forward(85)
115-
tracer(True)
116-
second_hand.setheading(6*sekunde)
117-
minute_hand.setheading(6*minute)
118-
hour_hand.setheading(30*stunde)
119-
tracer(True)
120-
ontimer(tick, 100)
106+
try:
107+
tracer(False) # Terminator can occur here
108+
writer.clear()
109+
writer.home()
110+
writer.forward(65)
111+
writer.write(wochentag(t),
112+
align="center", font=("Courier", 14, "bold"))
113+
writer.back(150)
114+
writer.write(datum(t),
115+
align="center", font=("Courier", 14, "bold"))
116+
writer.forward(85)
117+
tracer(True)
118+
second_hand.setheading(6*sekunde) # or here
119+
minute_hand.setheading(6*minute)
120+
hour_hand.setheading(30*stunde)
121+
tracer(True)
122+
ontimer(tick, 100)
123+
except Terminator:
124+
pass # turtledemo user pressed STOP
121125

122126
def main():
123127
tracer(False)

Lib/turtledemo/demohelp.txt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@
6060
be executed by the viewer (see provided example scripts)
6161
main() may return a string which will be displayed
6262
in the Label below the source code window (when execution
63-
has finished.)
63+
has finished.)
6464

65-
!! For programs, which are EVENT DRIVEN, main must return
66-
!! the string "EVENTLOOP". This informs the viewer, that the
67-
!! script is still running and must be stopped by the user!
65+
If the demo is EVENT DRIVEN, main must return the string
66+
"EVENTLOOP". This informs the demo viewer that the script is
67+
still running and must be stopped by the user!
68+
69+
If an "EVENTLOOP" demo runs by itself, as with clock, which uses
70+
ontimer, or minimal_hanoi, which loops by recursion, then the
71+
code should catch the turtle.Terminator exception that will be
72+
raised when the user presses the STOP button. (Paint is not such
73+
a demo; it only acts in response to mouse clicks and movements.)
6874

69-
70-

Lib/turtledemo/minimal_hanoi.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
---------------------------------------
1919
"""
2020
from turtle import *
21+
from turtle import Terminator # not in __all__
2122

2223
class Disc(Turtle):
2324
def __init__(self, n):
@@ -50,9 +51,12 @@ def hanoi(n, from_, with_, to_):
5051
def play():
5152
onkey(None,"space")
5253
clear()
53-
hanoi(6, t1, t2, t3)
54-
write("press STOP button to exit",
55-
align="center", font=("Courier", 16, "bold"))
54+
try:
55+
hanoi(6, t1, t2, t3)
56+
write("press STOP button to exit",
57+
align="center", font=("Courier", 16, "bold"))
58+
except Terminator:
59+
pass # turtledemo user pressed STOP
5660

5761
def main():
5862
global t1, t2, t3

0 commit comments

Comments
 (0)