如何更改 python 龟窗口的大小? [英] How can I change the size of my python turtle window?

查看:40
本文介绍了如何更改 python 龟窗口的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently trying to draw a Mandelbrot set in python with turtle. However, my problem has nothing to do with the Mandelbrot. I can't change the size of my turtle window. How can I do that?

我尝试初始化屏幕并使用 screensize 方法设置屏幕大小.如果我这样做,没有任何改变.

I tried to initialize a screen and set the screen size with the screensize method. Nothing changes if I do this.

这是我绘制集合的代码.我粘贴了整个代码,因为我不知道我做错了什么,屏幕尺寸没有改变.

This is my code for drawing the set. I pasted the whole code because I don't know what I did wrong that the screen size doesn't change.

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()

推荐答案

代替:

screen.screensize(width, height)

做:

screen.setup(width, height)

screensize() 方法设置海龟可以漫游的区域量,但不会改变屏幕大小(尽管名称不同),只是可滚动区域.此外,为了简化您的代码、加快速度并产生更详细的结果,我建议进行以下返工:

The screensize() method sets the amount of area the turtle can roam, but doesn't change the screen size (despite the name), just the scrollable area. Also, to simplify your code, speed it up and produce a more detailed result, I suggest the following rework:

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()

这篇关于如何更改 python 龟窗口的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆