Skip to content

Commit c124577

Browse files
authored
gh-123370: Fix the canvas not clearing after running turtledemo.clock (#123457)
Rewriting the day and date every tick somehow prevented them from being removed either by clicking STOP or loading another example. The solution is to rewrite them only when they change.
1 parent 528bbab commit c124577

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

Lib/turtledemo/clock.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
# -*- coding: cp1252 -*-
21
""" turtle-example-suite:
32
4-
tdemo_clock.py
3+
turtledemo/clock.py
54
65
Enhanced clock-program, showing date
76
and time
@@ -12,6 +11,9 @@
1211
from turtle import *
1312
from datetime import datetime
1413

14+
dtfont = "TkFixedFont", 14, "bold"
15+
current_day = None
16+
1517
def jump(distanz, winkel=0):
1618
penup()
1719
right(winkel)
@@ -52,11 +54,23 @@ def clockface(radius):
5254
jump(-radius)
5355
rt(6)
5456

57+
def display_date_time():
58+
global current_day
59+
writer.clear()
60+
now = datetime.now()
61+
current_day = now.day
62+
writer.home()
63+
writer.forward(distance=65)
64+
writer.write(wochentag(now), align="center", font=dtfont)
65+
writer.back(distance=150)
66+
writer.write(datum(now), align="center", font=dtfont)
67+
writer.forward(distance=85)
68+
5569
def setup():
5670
global second_hand, minute_hand, hour_hand, writer
5771
mode("logo")
5872
make_hand_shape("second_hand", 125, 25)
59-
make_hand_shape("minute_hand", 130, 25)
73+
make_hand_shape("minute_hand", 115, 25)
6074
make_hand_shape("hour_hand", 90, 25)
6175
clockface(160)
6276
second_hand = Turtle()
@@ -74,10 +88,10 @@ def setup():
7488
hand.speed(0)
7589
ht()
7690
writer = Turtle()
77-
#writer.mode("logo")
7891
writer.ht()
7992
writer.pu()
8093
writer.bk(85)
94+
display_date_time()
8195

8296
def wochentag(t):
8397
wochentag = ["Monday", "Tuesday", "Wednesday",
@@ -99,18 +113,11 @@ def tick():
99113
stunde = t.hour + minute/60.0
100114
try:
101115
tracer(False) # Terminator can occur here
102-
writer.clear()
103-
writer.home()
104-
writer.forward(65)
105-
writer.write(wochentag(t),
106-
align="center", font=("Courier", 14, "bold"))
107-
writer.back(150)
108-
writer.write(datum(t),
109-
align="center", font=("Courier", 14, "bold"))
110-
writer.forward(85)
111116
second_hand.setheading(6*sekunde) # or here
112117
minute_hand.setheading(6*minute)
113118
hour_hand.setheading(30*stunde)
119+
if t.day != current_day:
120+
display_date_time()
114121
tracer(True)
115122
ontimer(tick, 100)
116123
except Terminator:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix the canvas not clearing after running turtledemo clock.

0 commit comments

Comments
 (0)