如何更改python乌龟窗口的大小?

时间:2019-06-10 14:09:05

标签: python turtle-graphics screen-size

我目前正在尝试用Turtle在python中绘制Mandelbrot集。但是,我的问题与Mandelbrot无关。我无法更改乌龟窗口的大小。我该怎么办?

我试图初始化屏幕,并使用screensize方法设置屏幕大小。如果执行此操作,则没有任何改变。

这是我绘制场景的代码。我粘贴了整个代码,因为我不知道我做错了什么,因为屏幕尺寸没有变化。

from turtle import *


height = 360
width = 360
screen = Screen()
screen.screensize(width, height)


tu = Turtle()
tu.hideturtle()
tu.speed(0)
tu.penup()


def decreasePoint(n, start1, stop1, start2, stop2):
    return ((n - start1) / (stop1 - start1)) * (stop2 - start2) + start2


for x in range(width):
    for y in range(height):

        a = decreasePoint(x, 0, width, -2, 2)
        b = decreasePoint(y, 0, height, -2, 2)
        ca = a
        cb = b

        n = 0
        z = 0
        while n < 100:
            aa = a * a - b * b
            bb = 2 * a * b

            a = aa + ca
            b = bb + cb
            n += 1

            if abs(a + b) > 16:
                break
        bright = 'pink'
        if (n == 100):
            bright = 'black'

        tu.goto(x , y)
        tu.pendown()
        tu.dot(4, bright)
        tu.penup()
done()

1 个答案:

答案 0 :(得分:0)

代替:

screen.screensize(width, height)

这样做:

screen.setup(width, height)

screensize()方法设置乌龟可以漫游的区域数量,但不更改屏幕大小(尽管名称),仅更改可滚动区域。另外,为简化代码,加快代码速度并产生更详细的结果,我建议进行以下重新制作:

from turtle import Screen, Turtle

WIDTH, HEIGHT = 360, 360

screen = Screen()
screen.setup(WIDTH + 4, HEIGHT + 8)  # fudge factors due to window borders & title bar
screen.setworldcoordinates(0, 0, WIDTH, HEIGHT)

turtle = Turtle()
turtle.hideturtle()
turtle.penup()

def scalePoint(n, start1, stop1, start2, stop2):
    return (n - start1) * (stop2 - start2) / (stop1 - start1)  + start2

screen.tracer(False)

for x in range(WIDTH):
    real = scalePoint(x, 0, WIDTH, -2, 2)

    for y in range(HEIGHT):

        imaginary = scalePoint(y, 0, HEIGHT, -2, 2)

        c = complex(real, imaginary)

        z = 0j

        color = 'pink'

        for _ in range(100):
            if abs(z) >= 16.0:
                break

            z = z * z + c
        else:
            color = 'black'

        turtle.goto(x, y)
        turtle.dot(1, color)

    screen.update()

screen.tracer(True)
screen.exitonclick()