Problem when resizing and saving a turtle screen

假装没事ソ 提交于 2019-12-22 21:21:54

问题


So I could save a big drawing and see its full size in an image visualizer, I resized my turtle window bigger then my monitor size. But the saved image is not being resized, so the drawing is being truncated:

from turtle import Screen, Turtle
import random

screen = Screen()
screen.setup(width=1200, height=2700, startx=None, starty=None)
t = Turtle(visible=False)
t.speed('fastest')  # because I have no patience

t2 = Turtle(visible=False)
t2.speed('fastest')  # because I have no patience

t3 = Turtle(visible=False)
t3.speed('fastest')  # because I have no patience


def got(x, y, d):  # to use goto more easily
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.seth(d)


def flatoval(r):  # Horizontal Oval
    t.right(45)
    for loop in range(2):
        t.circle(r, 90)
        t.circle(r / 2, 90)


got(0, -200, 0)


def elipse(r, a, b, c):
    for extent in range(9):
        rnd = random.randint(1, 20)
        # if extent == 0 or extent == 3 or extent == 6 :
        # t.color('red')
        # if extent == 1 or extent == 4 or extent == 7 :
        # t.color('yellow')
        # if extent == 2 or extent == 5 or extent == 8 :
        # t.color('blue')

        t.circle(r, 10)

        heading = t.heading()

        if extent == 0 or extent == 1 or extent == 2:
            # t.color('green')
            t.setheading(0)
            t.forward(rnd)
            t.forward(a)
            t.backward(rnd)
            t.forward(c)

        t.setheading(heading)


def canais(x, y, d, egnar):
    for tog in range(egnar):
        got(x, y, d)

        elipse(100, 0, 0, 0)
        elipse(50, 0, 0, 0)
        elipse(100, 0, 0, 0)
        elipse(50, 0, 0, 0)
        d = d + 10
        elipse(200, 0, 0, 0)
        elipse(100, 0, 0, 0)
        elipse(200, 0, 0, 0)
        elipse(100, 0, 0, 0)

        elipse(300, 0, 0, 0)
        elipse(200, 0, 0, 0)
        elipse(300, 0, 0, 0)
        elipse(200, 0, 0, 0)


canais(0, -100, 0, 40)

ts = t.getscreen()

ts.getcanvas().postscript(file="canais_organizados_separadamente.eps")

I also tried this change:

screen = Screen()
screen.setup(width=1200, height=2700, startx=None, starty=None)

in place of:

screen = Screen()
screen.setup(400, 500)

Truncated image:


回答1:


By default, the tkinter canvas postscript() method only captures the visible portion of the canvas. You need to tell it, via the width and height arguments, whether you want more than that. Below is your code reworked with that fix and several others to improve the performance and/or simplify the logic:

from turtle import Screen, Turtle
from random import randint

def got(x, y, d):  # to use goto more easily
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.setheading(d)

def flatoval(r):  # Horizontal Oval
    turtle.right(45)

    for _ in range(2):
        turtle.circle(r, 90)
        turtle.circle(r / 2, 90)

def elipse(r, a, b, c):
    for extent in range(9):
        rnd = randint(1, 20)

        turtle.circle(r, 10)

        heading = turtle.heading()

        if extent <= 2:
            turtle.setheading(0)
            turtle.forward(rnd)
            turtle.forward(a)
            turtle.backward(rnd)
            turtle.forward(c)

        turtle.setheading(heading)

def canais(x, y, d, egnar):
    for _ in range(egnar):
        got(x, y, d)

        elipse(100, 0, 0, 0)
        elipse(50, 0, 0, 0)
        elipse(100, 0, 0, 0)
        elipse(50, 0, 0, 0)

        elipse(200, 0, 0, 0)
        elipse(100, 0, 0, 0)
        elipse(200, 0, 0, 0)
        elipse(100, 0, 0, 0)

        elipse(300, 0, 0, 0)
        elipse(200, 0, 0, 0)
        elipse(300, 0, 0, 0)
        elipse(200, 0, 0, 0)

        d += 10

screen = Screen()
screen.setup(1200, 1200)

turtle = Turtle(visible=False)

got(0, -200, 0)

screen.tracer(False)
canais(0, -100, 0, 36)
screen.tracer(True)

canvas = screen.getcanvas()

canvas.postscript(file="canais_organizados_separadamente.eps", width=1200, height=1200)



来源:https://stackoverflow.com/questions/55668231/problem-when-resizing-and-saving-a-turtle-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!