简体   繁体   中英

How to save and restore turtle-graphics draw parameters into/from a file?

是否可以在 Turtle 中绘制一些东西,将绘制参数保存在文件或数据库中,退出应用程序并使用到目前为止绘制的图片重新打开它?

In case anyone is interested, I have now solved my problem as follows: since i'm still new to python, i'm still not quite comfortable with the wrapper function. Graphics drawn with Turtle and saved as PNG every time I run through it. So now I have the overview where I am and how I have to continue, if I close the application

import turtle
from turtle import ScrolledCanvas, RawTurtle, TurtleScreen
from random import randint
from tkinter import *
from PIL import Image
import time
root = Tk()
cnt = 0
canvas = Canvas(root, width=800, height=800)
canvas.pack()

myPen = RawTurtle(canvas)
myPen.speed("fastest")

def drawTree(branchLen, t, size):
    global cnt
    cnt += 1
    makePicture(myPen, cnt)
    if branchLen > 5:
        if branchLen < 20:
            t.color("green")
        if size < 0:
            size = 1
            rsize = size
        else:
            rsize = size + 1

        myPen.pensize(size)
        tangle = 20
        t.forward(branchLen)
        t.right(tangle)
        drawTree(branchLen - randint(5, 15), t, size - 1)

        t.left(2 * tangle)
        drawTree(branchLen - randint(5, 15), t, size - 1)
        t.right(tangle)

        t.backward(branchLen)
        myPen.pensize(rsize)
        t.color("black")


def makePicture(myPen, cnt):
    nameeps = "tree.eps"
    ts = myPen.getscreen()
    ts.getcanvas().postscript(file=nameeps)
    saveImg(nameeps, cnt)

def saveImg(nameeps,cnt):
    image = "tree{}.png".format(cnt)
    canvas.postscript(file=nameeps, colormode='color')
    imgNew = Image.open(nameeps)
    imgNew.convert("RGBA")
    imgNew.thumbnail((2000,2000), Image.ANTIALIAS)
    imgNew.save(image, quality=95)

myPen.hideturtle()
myPen.penup()
size = 10
myPen.pensize(size)
myPen.goto(-50, -300)
myPen.pendown()
myPen.left(90)
drawTree(100, myPen, size)
root.mainloop()

This is just an example code I have tried around

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM