Kaydet (Commit) f51531e2 authored tarafından Terry Jan Reedy's avatar Terry Jan Reedy

Issue #21823: Catch turtle.Terminator exceptions in turtledemo.

Add note to demohelp.txt about doing so.
üst fabefc3c
...@@ -11,6 +11,7 @@ and time ...@@ -11,6 +11,7 @@ and time
------------------------------------ ------------------------------------
""" """
from turtle import * from turtle import *
from turtle import Terminator # not in __all__
from datetime import datetime from datetime import datetime
mode("logo") mode("logo")
...@@ -102,7 +103,8 @@ def tick(): ...@@ -102,7 +103,8 @@ def tick():
sekunde = t.second + t.microsecond*0.000001 sekunde = t.second + t.microsecond*0.000001
minute = t.minute + sekunde/60.0 minute = t.minute + sekunde/60.0
stunde = t.hour + minute/60.0 stunde = t.hour + minute/60.0
tracer(False) try:
tracer(False) # Terminator can occur here
writer.clear() writer.clear()
writer.home() writer.home()
writer.forward(65) writer.forward(65)
...@@ -113,11 +115,13 @@ def tick(): ...@@ -113,11 +115,13 @@ def tick():
align="center", font=("Courier", 14, "bold")) align="center", font=("Courier", 14, "bold"))
writer.forward(85) writer.forward(85)
tracer(True) tracer(True)
second_hand.setheading(6*sekunde) second_hand.setheading(6*sekunde) # or here
minute_hand.setheading(6*minute) minute_hand.setheading(6*minute)
hour_hand.setheading(30*stunde) hour_hand.setheading(30*stunde)
tracer(True) tracer(True)
ontimer(tick, 100) ontimer(tick, 100)
except Terminator:
pass # turtledemo user pressed STOP
def main(): def main():
tracer(False) tracer(False)
......
...@@ -62,9 +62,13 @@ ...@@ -62,9 +62,13 @@
in the Label below the source code window (when execution in the Label below the source code window (when execution
has finished.) has finished.)
!! For programs, which are EVENT DRIVEN, main must return If the demo is EVENT DRIVEN, main must return the string
!! the string "EVENTLOOP". This informs the viewer, that the "EVENTLOOP". This informs the demo viewer that the script is
!! script is still running and must be stopped by the user! 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.)
...@@ -18,6 +18,7 @@ stretched to rectangles by shapesize() ...@@ -18,6 +18,7 @@ stretched to rectangles by shapesize()
--------------------------------------- ---------------------------------------
""" """
from turtle import * from turtle import *
from turtle import Terminator # not in __all__
class Disc(Turtle): class Disc(Turtle):
def __init__(self, n): def __init__(self, n):
...@@ -50,9 +51,12 @@ def hanoi(n, from_, with_, to_): ...@@ -50,9 +51,12 @@ def hanoi(n, from_, with_, to_):
def play(): def play():
onkey(None,"space") onkey(None,"space")
clear() clear()
try:
hanoi(6, t1, t2, t3) hanoi(6, t1, t2, t3)
write("press STOP button to exit", write("press STOP button to exit",
align="center", font=("Courier", 16, "bold")) align="center", font=("Courier", 16, "bold"))
except Terminator:
pass # turtledemo user pressed STOP
def main(): def main():
global t1, t2, t3 global t1, t2, t3
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment