[Edu-sig] News about turtle graphics

Paul D. Fernhout pdfernhout at kurtz-fernhout.com
Fri Jun 30 07:56:00 CEST 2006


Gregor Lingl wrote:
> I've decided, that my new turtle graphics module ready
> now. 

Gregor-

Nice work!

Just for fun, I've integrated xturle.py with PataPata as a component, and 
here are a few changes I made to it (in diff format, at the end of this 
message), in case you are interested. Using PataPata, I was able to 
interactively build a xturtle script which took parameters, where the 
parameters were entered via Morphs I made as I needed them (like 
iterations, or color). I have an example world file with two widgets. Now, 
obviously, such an activity is not for the beginner user -- but more 
advanced ones might like being able to make parameterized xturtle functions.

Since I use Python2.3 mainly as it is the default install for Debian, I 
made xturtle.py run with Python2.3 by changing the static methods and the 
use of sorted(). There might be more Python2.3 issues, but those were the 
only ones I've come across so far.

I also added a line of code to reset TurtleScreen._RUNNING before throwing 
the Terminator exception, as otherwise you can't restart the turtle if you 
are using the library in a long running application. When I started 
playing around with it, that was a big issue for me, so that motivated my 
adding a BREAK interrupt key response to PataPata (though it only works 
for the xturtle XTurtleCanvas -- an endless loop doing nothing or just 
printing will still hang TK and PataPata).

Now, whether you want to make it run with Python2.3 may be debatable, and 
you may well use those static methods in other code, so making them 
instance methods may well break other demos. But you most probably want to 
reset the _Running flag in any case.

PataPata 0.1.19 has the XTurtleCanvas.py component in it, but the extra 
BREAK handling code is only in the latest stuff I've been adding to SVN here:
   http://svn.sourceforge.net/viewcvs.cgi/patapata/trunk/PataPata/

By the way, here is a simple world file written by PataPata that uses an 
XTurtleCanvas:
http://svn.sourceforge.net/viewcvs.cgi/patapata/trunk/PataPata/WorldWithXTurtlePC.py?view=markup
Note how the color and iterations are taken from GUI widget Morphs in the 
section:

     def mouseClick(self):
         reset()
         down()
         color(self.components.colorEntry.text)
         for i in range(int(self.components.iterationsEntry.text)):
             forward(100)
             right(91)
             forward(50)

This is the file that defines the XTurtleCanvas (among other things):
http://svn.sourceforge.net/viewcvs.cgi/patapata/trunk/PataPata/WorldCommonPC.py?view=markup
This is the relevant xturtle specific code to make an XTurtleCanvas:

     XTurtleCanvas = world.newPrototype("Morph")
     XTurtleCanvas.size = (200, 200)
     XTurtleCanvas.pen = None
     def _newWidget(self):
         xturtle._root = self.world.widget
         newCanvas = xturtle.ScrolledCanvas(self.container.widget)
         xturtle._canvas = newCanvas
         self.setAttributeWithoutSideEffects("pen", xturtle.Pen())
         xturtle._pen = self.pen
         return newCanvas
     XTurtleCanvas._newWidget = _newWidget
     def _destroyEmbeddedWidget(self):
         if self.widget:
             if xturtle._canvas == self.widget:
                 xturtle._canvas = None
             if xturtle.pen == self.pen:
                 xturtle._pen = None
             self.widget.destroy()
             self.widget = None
             self.setAttributeWithoutSideEffects("pen", None)
     XTurtleCanvas._destroyEmbeddedWidget = _destroyEmbeddedWidget
     def windowFocusIn(self):
         if self.widget:
             xturtle._canvas = self.widget
             xturtle._pen = self.pen
     XTurtleCanvas.windowFocusIn = windowFocusIn
     XTurtleCanvas.localProperty("pen").writeFunction = "_store_None"
     XTurtleCanvas.localProperty("pen").copyFunction = "_store_None"
     XTurtleCanvas.localProperty("pen").setFunction = None
     world.worldGlobals["XTurtleCanvas"] = XTurtleCanvas

(The bit at the end with properties just ensures the pen is read only and 
is stored as None when the world is saved to a Python file).

It was a bit tricky to get the thing to work right with those global 
functions when you have multiple canvases! I'm still not sure I'm quite 
satisfied with the result, but basically I swap in _pen and _canvas into 
the loaded xturtle module from the window's xturtle canvas whenever a TK 
window gets focus (I had to add that window focus handling too). This 
would be a problem if there was more than one xturtle canvas on a window, 
but by then for multiple canvases on one window I assume one could write 
code that accessed the each canvas's pen directly instead of using those 
globals functions.

Good luck with your talk. Thanks for making such a nice library.

--Paul Fernhout

$ diff xturtle.py ~/workspace/PataPata/xturtle.py
20a21,22
 > # PDF -- Modified to work with Python2.3 instead of 2.4 (removed static 
methods  use and sorted() use)
 > # PDF -- added "TurtleScreen._RUNNING = True" before raised Terminator.
292,294c294,297
<
<     @staticmethod
<     def _blankimage():
---
 >
 >     # pdf modified next two methods to not be static so coudl run with 
Python 2.3
 >     #@staticmethod
 >     def _blankimage(self):
301,302c304,305
<     @staticmethod
<     def _image(filename):
---
 >     #@staticmethod
 >     def _image(self, filename):
795a799
 >             TurtleScreen._RUNNING = True
864,865c868,872
<         """
<         return sorted(self._shapes.keys())
---
 >         """
 >         # PDF MODIFIED work with Python 2.3
 >         keys = self._shapes.keys()
 >         keys.sort()
 >         return keys


More information about the Edu-sig mailing list