summaryrefslogtreecommitdiff
path: root/Lib/turtle.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2008-09-29 22:19:08 +0000
committerMartin v. Löwis <martin@v.loewis.de>2008-09-29 22:19:08 +0000
commit263012346b10daabf632a11dd198c244e538c7bd (patch)
tree5aa5a5adc6160e74356e808e5066db9b4afae2f6 /Lib/turtle.py
parentd6c4b06c906ae6a9a832ef3ee07f766e3cb8e3c4 (diff)
downloadcpython-263012346b10daabf632a11dd198c244e538c7bd.tar.gz
Merged revisions 66686 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r66686 | martin.v.loewis | 2008-09-30 00:09:07 +0200 (Di, 30 Sep 2008) | 5 lines Issue #3965: Allow repeated calls to turtle.Screen, by making it a true singleton object. Reviewed by Gregor Lingl. ........
Diffstat (limited to 'Lib/turtle.py')
-rw-r--r--Lib/turtle.py59
1 files changed, 31 insertions, 28 deletions
diff --git a/Lib/turtle.py b/Lib/turtle.py
index 1497d2d123..bcc53ce3dd 100644
--- a/Lib/turtle.py
+++ b/Lib/turtle.py
@@ -2421,7 +2421,7 @@ class RawTurtle(TPen, TNavigator):
shape=_CFG["shape"],
undobuffersize=_CFG["undobuffersize"],
visible=_CFG["visible"]):
- if isinstance(canvas, Screen):
+ if isinstance(canvas, _Screen):
self.screen = canvas
elif isinstance(canvas, TurtleScreen):
if canvas not in RawTurtle.screens:
@@ -3558,29 +3558,33 @@ class RawTurtle(TPen, TNavigator):
RawPen = RawTurtle
-### Screen - Klasse ########################
+### Screen - Singleton ########################
-class Screen(TurtleScreen):
+def Screen():
+ """Return the singleton screen object.
+ If none exists at the moment, create a new one and return it,
+ else return the existing one."""
+ if Turtle._screen is None:
+ Turtle._screen = _Screen()
+ return Turtle._screen
+
+class _Screen(TurtleScreen):
_root = None
_canvas = None
_title = _CFG["title"]
- # Borg-Idiom
-
- _shared_state = {}
-
- def __new__(cls, *args, **kwargs):
- obj = object.__new__(cls, *args, **kwargs)
- obj.__dict__ = cls._shared_state
- return obj
-
def __init__(self):
- if Screen._root is None:
- Screen._root = self._root = _Root()
- self._root.title(Screen._title)
+ # XXX there is no need for this code to be conditional,
+ # as there will be only a single _Screen instance, anyway
+ # XXX actually, the turtle demo is injecting root window,
+ # so perhaps the conditional creation of a root should be
+ # preserved (perhaps by passing it as an optional parameter)
+ if _Screen._root is None:
+ _Screen._root = self._root = _Root()
+ self._root.title(_Screen._title)
self._root.ondestroy(self._destroy)
- if Screen._canvas is None:
+ if _Screen._canvas is None:
width = _CFG["width"]
height = _CFG["height"]
canvwidth = _CFG["canvwidth"]
@@ -3588,10 +3592,9 @@ class Screen(TurtleScreen):
leftright = _CFG["leftright"]
topbottom = _CFG["topbottom"]
self._root.setupcanvas(width, height, canvwidth, canvheight)
- Screen._canvas = self._root._getcanvas()
+ _Screen._canvas = self._root._getcanvas()
self.setup(width, height, leftright, topbottom)
- TurtleScreen.__init__(self, Screen._canvas)
- Turtle._screen = self
+ TurtleScreen.__init__(self, _Screen._canvas)
def setup(self, width=_CFG["width"], height=_CFG["height"],
startx=_CFG["leftright"], starty=_CFG["topbottom"]):
@@ -3645,17 +3648,17 @@ class Screen(TurtleScreen):
Example (for a Screen instance named screen):
>>> screen.title("Welcome to the turtle-zoo!")
"""
- if Screen._root is not None:
- Screen._root.title(titlestring)
- Screen._title = titlestring
+ if _Screen._root is not None:
+ _Screen._root.title(titlestring)
+ _Screen._title = titlestring
def _destroy(self):
root = self._root
- if root is Screen._root:
+ if root is _Screen._root:
Turtle._pen = None
Turtle._screen = None
- Screen._root = None
- Screen._canvas = None
+ _Screen._root = None
+ _Screen._canvas = None
TurtleScreen._RUNNING = True
root.destroy()
@@ -3747,7 +3750,7 @@ def write_docstringdict(filename="turtle_docstringdict"):
docsdict = {}
for methodname in _tg_screen_functions:
- key = "Screen."+methodname
+ key = "_Screen."+methodname
docsdict[key] = eval(key).__doc__
for methodname in _tg_turtle_functions:
key = "Turtle."+methodname
@@ -3862,7 +3865,7 @@ def _screen_docrevise(docstr):
for methodname in _tg_screen_functions:
- pl1, pl2 = getmethparlist(eval('Screen.' + methodname))
+ pl1, pl2 = getmethparlist(eval('_Screen.' + methodname))
if pl1 == "":
print(">>>>>>", pl1, pl2)
continue
@@ -3870,7 +3873,7 @@ for methodname in _tg_screen_functions:
{'key':methodname, 'pl1':pl1, 'pl2':pl2})
## print("Screen:", defstr)
exec(defstr)
- eval(methodname).__doc__ = _screen_docrevise(eval('Screen.'+methodname).__doc__)
+ eval(methodname).__doc__ = _screen_docrevise(eval('_Screen.'+methodname).__doc__)
for methodname in _tg_turtle_functions:
pl1, pl2 = getmethparlist(eval('Turtle.' + methodname))