diff options
Diffstat (limited to 'Lib/turtle.py')
-rw-r--r-- | Lib/turtle.py | 103 |
1 files changed, 55 insertions, 48 deletions
diff --git a/Lib/turtle.py b/Lib/turtle.py index c6ebfa5c6d..cbd4f47df9 100644 --- a/Lib/turtle.py +++ b/Lib/turtle.py @@ -109,6 +109,7 @@ import types import math import time import inspect +import sys from os.path import isfile, split, join from copy import deepcopy @@ -139,7 +140,7 @@ _tg_turtle_functions = ['back', 'backward', 'begin_fill', 'begin_poly', 'bk', _tg_utilities = ['write_docstringdict', 'done'] __all__ = (_tg_classes + _tg_screen_functions + _tg_turtle_functions + - _tg_utilities) # + _math_functions) + _tg_utilities + ['Terminator']) # + _math_functions) _alias_list = ['addshape', 'backward', 'bk', 'fd', 'ht', 'lt', 'pd', 'pos', 'pu', 'rt', 'seth', 'setpos', 'setposition', 'st', @@ -992,6 +993,13 @@ class TurtleScreen(TurtleScreenBase): self._colormode = _CFG["colormode"] self._keys = [] self.clear() + if sys.platform == 'darwin': + # Force Turtle window to the front on OS X. This is needed because + # the Turtle window will show behind the Terminal window when you + # start the demo from the command line. + rootwindow = cv.winfo_toplevel() + rootwindow.call('wm', 'attributes', '.', '-topmost', '1') + rootwindow.call('wm', 'attributes', '.', '-topmost', '0') def clear(self): """Delete all drawings and all turtles from the TurtleScreen. @@ -1280,7 +1288,7 @@ class TurtleScreen(TurtleScreenBase): def _incrementudc(self): """Increment update counter.""" if not TurtleScreen._RUNNING: - TurtleScreen._RUNNNING = True + TurtleScreen._RUNNING = True raise Terminator if self._tracing > 0: self._updatecounter += 1 @@ -2587,7 +2595,7 @@ class RawTurtle(TPen, TNavigator): Example (for a Turtle instance named turtle): >>> turtle.setundobuffer(42) """ - if size is None: + if size is None or size <= 0: self.undobuffer = None else: self.undobuffer = Tbuffer(size) @@ -2938,7 +2946,7 @@ class RawTurtle(TPen, TNavigator): self._stretchfactor = a11, a22 self._shearfactor = a12/a22 self._tilt = alfa - self._update() + self.pen(resizemode="user") def _polytrafo(self, poly): @@ -3746,7 +3754,7 @@ class _Screen(TurtleScreen): Turtle._screen = None _Screen._root = None _Screen._canvas = None - TurtleScreen._RUNNING = True + TurtleScreen._RUNNING = False root.destroy() def bye(self): @@ -3787,7 +3795,6 @@ class _Screen(TurtleScreen): except AttributeError: exit(0) - class Turtle(RawTurtle): """RawTurtle auto-creating (scrolled) canvas. @@ -3810,18 +3817,6 @@ class Turtle(RawTurtle): Pen = Turtle -def _getpen(): - """Create the 'anonymous' turtle if not already present.""" - if Turtle._pen is None: - Turtle._pen = Turtle() - return Turtle._pen - -def _getscreen(): - """Create a TurtleScreen if not already present.""" - if Turtle._screen is None: - Turtle._screen = Screen() - return Turtle._screen - def write_docstringdict(filename="turtle_docstringdict"): """Create and write docstring-dictionary to file. @@ -3843,18 +3838,18 @@ def write_docstringdict(filename="turtle_docstringdict"): key = "Turtle."+methodname docsdict[key] = eval(key).__doc__ - f = open("%s.py" % filename,"w") - keys = sorted([x for x in docsdict.keys() - if x.split('.')[1] not in _alias_list]) - f.write('docsdict = {\n\n') - for key in keys[:-1]: + with open("%s.py" % filename,"w") as f: + keys = sorted([x for x in docsdict.keys() + if x.split('.')[1] not in _alias_list]) + f.write('docsdict = {\n\n') + for key in keys[:-1]: + f.write('%s :\n' % repr(key)) + f.write(' """%s\n""",\n\n' % docsdict[key]) + key = keys[-1] f.write('%s :\n' % repr(key)) - f.write(' """%s\n""",\n\n' % docsdict[key]) - key = keys[-1] - f.write('%s :\n' % repr(key)) - f.write(' """%s\n"""\n\n' % docsdict[key]) - f.write("}\n") - f.close() + f.write(' """%s\n"""\n\n' % docsdict[key]) + f.write("}\n") + f.close() def read_docstrings(lang): """Read in docstrings from lang-specific docstring dictionary. @@ -3944,26 +3939,38 @@ def _screen_docrevise(docstr): ## as functions. So we can enhance, change, add, delete methods to these ## classes and do not need to change anything here. +__func_body = """\ +def {name}{paramslist}: + if {obj} is None: + if not TurtleScreen._RUNNING: + TurtleScreen._RUNNING = True + raise Terminator + {obj} = {init} + try: + return {obj}.{name}{argslist} + except TK.TclError: + if not TurtleScreen._RUNNING: + TurtleScreen._RUNNING = True + raise Terminator + raise +""" -for methodname in _tg_screen_functions: - pl1, pl2 = getmethparlist(eval('_Screen.' + methodname)) - if pl1 == "": - print(">>>>>>", pl1, pl2) - continue - defstr = ("def %(key)s%(pl1)s: return _getscreen().%(key)s%(pl2)s" % - {'key':methodname, 'pl1':pl1, 'pl2':pl2}) - exec(defstr) - eval(methodname).__doc__ = _screen_docrevise(eval('_Screen.'+methodname).__doc__) - -for methodname in _tg_turtle_functions: - pl1, pl2 = getmethparlist(eval('Turtle.' + methodname)) - if pl1 == "": - print(">>>>>>", pl1, pl2) - continue - defstr = ("def %(key)s%(pl1)s: return _getpen().%(key)s%(pl2)s" % - {'key':methodname, 'pl1':pl1, 'pl2':pl2}) - exec(defstr) - eval(methodname).__doc__ = _turtle_docrevise(eval('Turtle.'+methodname).__doc__) +def _make_global_funcs(functions, cls, obj, init, docrevise): + for methodname in functions: + method = getattr(cls, methodname) + pl1, pl2 = getmethparlist(method) + if pl1 == "": + print(">>>>>>", pl1, pl2) + continue + defstr = __func_body.format(obj=obj, init=init, name=methodname, + paramslist=pl1, argslist=pl2) + exec(defstr, globals()) + globals()[methodname].__doc__ = docrevise(method.__doc__) + +_make_global_funcs(_tg_screen_functions, _Screen, + 'Turtle._screen', 'Screen()', _screen_docrevise) +_make_global_funcs(_tg_turtle_functions, Turtle, + 'Turtle._pen', 'Turtle()', _turtle_docrevise) done = mainloop |