Skip to content

Commit 96f95df

Browse files
authored
Rewrite the turtledemo makeGraphFrame method (#104224)
Replace `self._canvas` and `self.scanvas`, both bound to `canvas`, with `self.canvas, which is accessed in other methods. Replace `_s_` with `screen` and `_s_._canvas` with `canvas`. Add a comment explaining the unorthodox use of function turtle.Screen and singleton class turtle._Screen.
1 parent e407661 commit 96f95df

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Lib/turtledemo/__main__.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ def __init__(self, filename=None):
203203

204204

205205
def onResize(self, event):
206-
cwidth = self._canvas.winfo_width()
207-
cheight = self._canvas.winfo_height()
208-
self._canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth)
209-
self._canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight)
206+
cwidth = self.canvas.winfo_width()
207+
cheight = self.canvas.winfo_height()
208+
self.canvas.xview_moveto(0.5*(self.canvwidth-cwidth)/self.canvwidth)
209+
self.canvas.yview_moveto(0.5*(self.canvheight-cheight)/self.canvheight)
210210

211211
def makeTextFrame(self, root):
212212
self.text_frame = text_frame = Frame(root)
@@ -237,19 +237,23 @@ def makeTextFrame(self, root):
237237
return text_frame
238238

239239
def makeGraphFrame(self, root):
240+
# t._Screen is a singleton class instantiated or retrieved
241+
# by calling Screen. Since tdemo canvas needs a different
242+
# configuration, we manually set class attributes before
243+
# calling Screen and manually call superclass init after.
240244
turtle._Screen._root = root
245+
241246
self.canvwidth = 1000
242247
self.canvheight = 800
243-
turtle._Screen._canvas = self._canvas = canvas = turtle.ScrolledCanvas(
248+
turtle._Screen._canvas = self.canvas = canvas = turtle.ScrolledCanvas(
244249
root, 800, 600, self.canvwidth, self.canvheight)
245250
canvas.adjustScrolls()
246251
canvas._rootwindow.bind('<Configure>', self.onResize)
247252
canvas._canvas['borderwidth'] = 0
248253

249-
self.screen = _s_ = turtle.Screen()
250-
turtle.TurtleScreen.__init__(_s_, _s_._canvas)
251-
self.scanvas = _s_._canvas
252-
turtle.RawTurtle.screens = [_s_]
254+
self.screen = screen = turtle.Screen()
255+
turtle.TurtleScreen.__init__(screen, canvas)
256+
turtle.RawTurtle.screens = [screen]
253257
return canvas
254258

255259
def set_txtsize(self, size):
@@ -373,7 +377,7 @@ def startDemo(self):
373377
def clearCanvas(self):
374378
self.refreshCanvas()
375379
self.screen._delete("all")
376-
self.scanvas.config(cursor="")
380+
self.canvas.config(cursor="")
377381
self.configGUI(NORMAL, DISABLED, DISABLED)
378382

379383
def stopIt(self):

0 commit comments

Comments
 (0)