Closed aroberge closed 4 years ago
Thanks for reporting and working on this!
Due to differences between SVG and Tkinter, there will definitely be differences between the Brython turtle module and the CPython one. For instance, whereas one can have an undo mode with CPython, used to remove objects added in an interactive session, the same cannot be done with Brython.
However, the counterpart can also be true: there are some features easily implemented in SVG that could not be done with the Tkinter-based turtle module. I will keep track here of some possible features to implement.
dot()
method.I have decided to postpone these additions for the near future.
@PierreQuentel Do you have any objections to me adding features to Brython's turtle module which are not available in CPython's version? The converse is going to be true: there are features of CPython's version that cannot be reproduced using Brython's version ( -- at least, I cannot see how to implement all the features, like the undo()
method). Having some additional features that can produce nicer looking graphics might counteract any negative impression caused by the missing features.
@aroberge Yes that's fine for me, can you write a documentation page with the major differences between the CPython version and this one ? There was a post on Reddit a few days ago about a program made with turtle, do you think it could work with the Brython version ?
@PierreQuentel That post on Reddit was my motivation to have a closer look at the turtle module. It is my goal to be able to reproduce it if possible - I still cannot even though I've re-implemented most of the missing functions.
As for the documentation: I'm adding a selection of demos (chosen from a drop down menu) including some that will document all the differences between the Brython version and the CPython version, listing all the methods from https://docs.python.org/3/library/turtle.html#turtle-methods
The reddit post code using my current version. I had forgotten to call the done()
method (which I previously named show()
) in my recent attempts - thus producing no output.
Just because people like pictures ... Using CPython's version, I set the pen width to 30 and drew a black line, and then set the pen width to 3 and drew the same distance over: the "line" drawn has rounded edges.
Using the current version with Brython, I get the following:
This likely explains the difference between the image I obtained using the code from the reddit post and the image posted there.
I will try to change the "line" drawing method to reproduce the CPython's case but add an additional option parameter (rounded_lines
or similar), set to True by default but could be changed by the user.
Can't resist posting an updated picture. :-)
Wow, nice!
Once the pull request is accepted, this issue can be closed as far as I am concerned.
The Pull Request has been merged. Encore merci André :-)
There seems to be a problem ... I've updated my local copy of the repository and the last example (Procedural version: comparison with CPython), it no longer works. I'll need to have a look at what went wrong.
EDIT: I know that this was working but, even after changing the branch to the one I used to work on, it still raises an exception ... and I do not understand why.
@aroberge I am reviewing old issues and wondering if keeping this one in the issue tracker still makes sense. What do you think ?
@PierreQuentel On peut fermer le tout.
The existing turtle module is an excellent adaptation of CPython's tkinter-based version that uses Scalable Vector Graphics (svg) for drawing including animations and is perfectly usable for simple demonstrations.
However, as I explored it, I found quite a few missing methods, some of which might not make much sense to implement in this browser version, but not all. I will document them here as I go along, as well as the progress done on a branch in my fork (https://github.com/aroberge/brython/tree/fix/master/turtle739).
document[_CFG["turtle_canvas_id"]]
can raise a KeyError exception if therestart()
function is called twice in a row.write
(https://github.com/brython-dev/brython/blob/master/www/src/Lib/turtle.py#L2777) should allow the user to set the font. However, the font information is not passed on tosvg.text
(https://github.com/brython-dev/brython/blob/master/www/src/Lib/turtle.py#L338). Similarly for thealign
parameter.left()
appears to be turning "right" instead.__main__
is irrelevant for Brython and has been removed.undo()
method which does not really makes sense in the current context. It, and all the associated objects (such asundobuffer
) will be removed.dot()
method does not produce a circle and appears to suspend any running program.write()
is the very first method called before any other turtle drawing, nothing gets written, nor drawn afterwards.bgcolor()
method does not do anything.stamp()
method does not do anything.begin_fill()
andend_fill()
are ignored.penup()
, the moving turtle is currently not drawn when moving.Efficiency consideration
This following should be done as a second pass (perhaps in a different branch) after all the relevant methods have been done.
Currently, each time a turtle is moved, a "new" turtle shape is created and drawn on the screen as a separate svg element, moved, and, unlike in the CPython case, disappears from the screen after a drawing is done. It should be possible to repeatedly use the turtles that have been created by:
fill
tofreeze
after an animation has been done.fill
toremove
when another animation is desired.fill
tofreeze
againIn addition to being more efficient, as we would reuse an existing svg element rather than creating and adding a new one each time, this would likely mimic better the behavior of the CPython turtle.
A further consideration might be to see if
clear
andreset
for a given turtle could not be implemented by setting the visibility attribute of previously drawn elements.