donkirkby / live-py-plugin

Live coding in Python with PyCharm, Emacs, Sublime Text, or even a browser
https://donkirkby.github.io/live-py-plugin
MIT License
292 stars 56 forks source link

[feature request] Could you help to implement dot in svg_turtle? #274

Closed lifubang closed 4 years ago

lifubang commented 4 years ago

What I did

[feature request] Could you help to implement dot in svg_turtle?

#!/usr/bin/env python  
# coding=utf-8 
import sys
from turtle import *  # @UnusedWildImport
import svgwrite
from svg_turtle import SvgTurtle

def write_file(draw_func, filename, size):
    drawing = svgwrite.Drawing(filename, size=size)
    drawing.add(drawing.rect(fill='white', size=('100%', '100%')))
    t = SvgTurtle(drawing)
    Turtle._screen = t.screen
    Turtle._pen = t
    draw_func()
    drawing.save()

def draw():
    a = [[120.71, 50], [50, 120.71], [-50, 120.71], [-120.71, 50], [-50, -20.71], [50, -20.71], [20.71, 50],
            [-20.71, 50]]
    pencolor("red")
    penup()
    for i in range(len(a)):
        goto(a[i])
        down()
        dot(10, "red")
        penup()

def main():
    write_file(draw, 'test.svg', size=('500px', '500px'))

if __name__ == '__main__':
    main()

What happened

When I run this code, got this error:

Traceback (most recent call last):
  File ".\data\test.py", line 32, in <module>
    main()
  File ".\data\test.py", line 29, in main
    write_file(draw, 'test.svg', size=('500px', '500px'))
  File ".\data\test.py", line 14, in write_file
    draw_func()
  File ".\data\test.py", line 25, in draw
    dot(10, "red")
  File "<string>", line 8, in dot
AttributeError: 'SvgTurtle' object has no attribute 'dot'

What I wanted to happen

Draw that dots in svg file

My environment

Describe the versions of everything you were using:

lifubang commented 4 years ago

The svg_turtle.py is in https://github.com/donkirkby/live-py-plugin/blob/master/test/PySrc/tools/svg_turtle.py

Thank you very much.

lifubang commented 4 years ago

@donkirkby Excuse me, could you PTAL? If you can help me, it will be a big help for us. Thank you.

donkirkby commented 4 years ago

Hi there! Thanks for pointing out the dot() method, I've never used that.

Are you asking for guidance on how to add the feature, or are you asking me to add the feature for you? If you're asking for guidance, I suggest you add a dot() method to the SvgTurtle class, and it should probably look a lot like the _draw_line() method, but call cv.circle() instead of cv.line().

Thanks for your interest in the project. I'd love to hear how you found out about it, and what you've used it for.

lifubang commented 4 years ago

I teach students to learn python turtle, so I need students to hand in their homework, they use svg_turtle to save the result of their code. I found out svg_turtle from search engine, I found that svg_turtle is used in wide range. So I think we should support more features in svg_turtle. So, If you can add dot in svg_turtle, I may add other methods in it based on your example.

donkirkby commented 4 years ago

I added the dot() method to SvgTurtle, and I added a bgcolor option to SvgTurtle.create(). That makes it easier to create the turtle object and save the file. Here's an updated version of your example:

#!/usr/bin/env python
# coding=utf-8
from turtle import *  # @UnusedWildImport
from svg_turtle import SvgTurtle

def write_file(draw_func, filename, size):
    t = SvgTurtle.create(*size, bgcolor='white')
    Turtle._screen = t.screen
    Turtle._pen = t
    draw_func()
    t.save_as(filename)

def draw():
    a = [[120.71, 50], [50, 120.71], [-50, 120.71], [-120.71, 50], [-50, -20.71], [50, -20.71], [20.71, 50],
         [-20.71, 50]]
    pencolor("red")
    penup()
    for pos in a:
        goto(pos)
        dot(10)

def main():
    write_file(draw, 'test.svg', size=('500px', '500px'))

if __name__ == '__main__':
    main()
lifubang commented 4 years ago

Thank you very much. I will test it ASAP.