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

Skip to content

Commit f5ac57d

Browse files
committed
Issue #21882: In turtle demos, remove module scope gui and sys calls by
either deleting or moving to the module's main function.
1 parent 98b6391 commit f5ac57d

7 files changed

Lines changed: 51 additions & 61 deletions

File tree

Lib/turtle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
_tg_utilities = ['write_docstringdict', 'done']
141141

142142
__all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions +
143-
_tg_utilities) # + _math_functions)
143+
_tg_utilities + ['Terminator']) # + _math_functions)
144144

145145
_alias_list = ['addshape', 'backward', 'bk', 'fd', 'ht', 'lt', 'pd', 'pos',
146146
'pu', 'rt', 'seth', 'setpos', 'setposition', 'st',

Lib/turtledemo/clock.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,8 @@
1111
------------------------------------
1212
"""
1313
from turtle import *
14-
from turtle import Terminator # not in __all__
1514
from datetime import datetime
1615

17-
mode("logo")
18-
1916
def jump(distanz, winkel=0):
2017
penup()
2118
right(winkel)
@@ -43,7 +40,6 @@ def make_hand_shape(name, laenge, spitze):
4340
hand_form = get_poly()
4441
register_shape(name, hand_form)
4542

46-
4743
def clockface(radius):
4844
reset()
4945
pensize(7)
@@ -84,7 +80,6 @@ def setup():
8480
writer.pu()
8581
writer.bk(85)
8682

87-
8883
def wochentag(t):
8984
wochentag = ["Monday", "Tuesday", "Wednesday",
9085
"Thursday", "Friday", "Saturday", "Sunday"]
@@ -131,6 +126,7 @@ def main():
131126
return "EVENTLOOP"
132127

133128
if __name__ == "__main__":
129+
mode("logo")
134130
msg = main()
135131
print(msg)
136132
mainloop()

Lib/turtledemo/colormixer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# colormixer
22

33
from turtle import Screen, Turtle, mainloop
4-
import sys
5-
sys.setrecursionlimit(20000) # overcomes, for now, an instability of Python 3.0
64

75
class ColorTurtle(Turtle):
86

Lib/turtledemo/demohelp.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
(2) How to add your own demos to the demo repository
5555

5656
- Place the file in the same directory as turtledemo/__main__.py
57+
IMPORTANT! When imported, the demo should not modify the system
58+
by calling functions in other modules, such as sys, tkinter, or
59+
turtle. Global variables should be initialized in main().
5760

5861
- The code must contain a main() function which will
5962
be executed by the viewer (see provided example scripts).
@@ -65,7 +68,7 @@
6568

6669
if __name__ == '__main__':
6770
main()
68-
mainloop() # keep window
71+
mainloop() # keep window open
6972

7073
python -m turtledemo.mydemo # will then run it
7174

Lib/turtledemo/minimal_hanoi.py

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

2322
class Disc(Turtle):
2423
def __init__(self, n):

Lib/turtledemo/nim.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ def display(self, msg1, msg2=None):
143143
self.writer.write(msg1, align="center", font=("Courier",14,"bold"))
144144
self.screen.tracer(True)
145145

146-
147146
def setup(self):
148147
self.screen.tracer(False)
149148
for row in range(3):
@@ -181,6 +180,7 @@ def clear(self):
181180
if self.game.state == Nim.OVER:
182181
self.screen.clear()
183182

183+
184184
class NimController(object):
185185

186186
def __init__(self, game):
@@ -201,6 +201,7 @@ def notify_move(self, row, col):
201201
self.game.model.notify_move(row, col)
202202
self.BUSY = False
203203

204+
204205
class Nim(object):
205206
CREATED = 0
206207
RUNNING = 1
@@ -213,11 +214,10 @@ def __init__(self, screen):
213214
self.controller = NimController(self)
214215

215216

216-
mainscreen = turtle.Screen()
217-
mainscreen.mode("standard")
218-
mainscreen.setup(SCREENWIDTH, SCREENHEIGHT)
219-
220217
def main():
218+
mainscreen = turtle.Screen()
219+
mainscreen.mode("standard")
220+
mainscreen.setup(SCREENWIDTH, SCREENHEIGHT)
221221
nim = Nim(mainscreen)
222222
return "EVENTLOOP!"
223223

Lib/turtledemo/two_canvases.py

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,54 @@
11
"""turtledemo.two_canvases
22
33
Use TurtleScreen and RawTurtle to draw on two
4-
distinct canvases.
4+
distinct canvases in a separate windows. The
5+
new window must be separately closed in
6+
addition to pressing the STOP button.
57
"""
6-
#The final mainloop only serves to keep the window open.
7-
8-
#TODO: This runs in its own two-canvas window when selected in the
9-
#demoviewer examples menu but the text is not loaded and the previous
10-
#example is left visible. If the ending mainloop is removed, the text
11-
#Eis loaded, this run again in a third window, and if start is pressed,
12-
#demoviewer raises an error because main is not found, and then freezes.
138

149
from turtle import TurtleScreen, RawTurtle, TK
1510

16-
root = TK.Tk()
17-
cv1 = TK.Canvas(root, width=300, height=200, bg="#ddffff")
18-
cv2 = TK.Canvas(root, width=300, height=200, bg="#ffeeee")
19-
cv1.pack()
20-
cv2.pack()
11+
def main():
12+
root = TK.Tk()
13+
cv1 = TK.Canvas(root, width=300, height=200, bg="#ddffff")
14+
cv2 = TK.Canvas(root, width=300, height=200, bg="#ffeeee")
15+
cv1.pack()
16+
cv2.pack()
2117

22-
s1 = TurtleScreen(cv1)
23-
s1.bgcolor(0.85, 0.85, 1)
24-
s2 = TurtleScreen(cv2)
25-
s2.bgcolor(1, 0.85, 0.85)
18+
s1 = TurtleScreen(cv1)
19+
s1.bgcolor(0.85, 0.85, 1)
20+
s2 = TurtleScreen(cv2)
21+
s2.bgcolor(1, 0.85, 0.85)
2622

27-
p = RawTurtle(s1)
28-
q = RawTurtle(s2)
23+
p = RawTurtle(s1)
24+
q = RawTurtle(s2)
2925

30-
p.color("red", (1, 0.85, 0.85))
31-
p.width(3)
32-
q.color("blue", (0.85, 0.85, 1))
33-
q.width(3)
26+
p.color("red", (1, 0.85, 0.85))
27+
p.width(3)
28+
q.color("blue", (0.85, 0.85, 1))
29+
q.width(3)
3430

35-
for t in p,q:
36-
t.shape("turtle")
37-
t.lt(36)
31+
for t in p,q:
32+
t.shape("turtle")
33+
t.lt(36)
3834

39-
q.lt(180)
35+
q.lt(180)
4036

41-
for t in p, q:
42-
t.begin_fill()
43-
for i in range(5):
4437
for t in p, q:
45-
t.fd(50)
46-
t.lt(72)
47-
for t in p,q:
48-
t.end_fill()
49-
t.lt(54)
50-
t.pu()
51-
t.bk(50)
52-
53-
## Want to get some info?
54-
55-
#print(s1, s2)
56-
#print(p, q)
57-
#print(s1.turtles())
58-
#print(s2.turtles())
59-
60-
TK.mainloop()
38+
t.begin_fill()
39+
for i in range(5):
40+
for t in p, q:
41+
t.fd(50)
42+
t.lt(72)
43+
for t in p,q:
44+
t.end_fill()
45+
t.lt(54)
46+
t.pu()
47+
t.bk(50)
48+
49+
return "EVENTLOOP"
50+
51+
52+
if __name__ == '__main__':
53+
main()
54+
TK.mainloop() # keep window open until user closes it

0 commit comments

Comments
 (0)