如何阻止 python 乌龟绘图

How to stop the python turtle from drawing

谁能告诉我为什么这段代码总是在屏幕上显示一行,以及如何停止它?

这有一个小问题,每次发生这种情况时,无论我尝试做什么,我的 canvas 上总是会出现一条线。关于如何防止这种情况的任何想法?

现在这可能是代码太多了,但我不知道还有什么方法可以显示失败的代码。我尝试了多种方法,但 none 似乎有效。

def drawpixel(x, y, colour):
    turtle.goto(x, y)
    turtle.dot(1, colour)

def main():
    screen = turtle.Screen()
    screen.title('Mandelbrot set drawer')
    screen.bgcolor('#22ccaa')
    turtle.hideturtle()
    turtle.speed(0)
    turtle.penup()
    turtle.tracer(-10000, 0)
    turtle.speed(0)
    width = int(turtle.numinput('Screen size', 'What width?', 600, 100, 20000))
    height = int(turtle.numinput('Screen size', 'What height?', 600, 100, 10000))
    turtle.setworldcoordinates((-width)//2, (-height)//2, (width)//2, (height)//2)
    radius = 2
    turtle.goto((-width)//2, (-height)//2)
    x, y = ((-width)//2, (-height)//2)
    while y<((height)//2 +1):
        while x!=((width)//2 +1):
            newx = x/(width//2)*radius
            newy = y/(width//2)*radius
            mpos = newx + newy*1j
            drawpixel(x, y, getcolour(mpos))
            x += 1
        y += 1
        turtle.goto((-width)//2, y)

也许 getcolour 失败了,所以这里是它的代码:

def iterc(c):
    iters = 0
    z = 0+0j
    while iters < 16 and math.hypot(z.real, z.imag)<2:
        z = z*z+c
        iters += 1
    return iters

def getcolour(pos):
    x = iterc(pos)
    colournum = int(x*6.25)
    colour = 'gray{}'.format(colournum)

我修改了您的代码以删除该行并修复了我看到的其他问题:

def main():
    screen = turtle.Screen()
    screen.title('Mandelbrot Set')
    screen.bgcolor('#22ccaa')

    width = int(screen.numinput('Screen size', 'What width?', 600, 100, 20000))
    height = int(screen.numinput('Screen size', 'What height?', 600, 100, 10000))
    screen.setworldcoordinates(-width // 2, -height // 2, width // 2, height // 2)
    screen.tracer(0, 0)

    radius = 2

    turtle.penup()
    turtle.hideturtle()
    turtle.speed('fastest')

    x, y = -width // 2, -height // 2
    turtle.goto(x, y)

    while y < (height // 2):

        while x < (width // 2):

            newx = x / (width // 2) * radius
            newy = y / (width // 2) * radius
            mpos = newx + newy * 1j
            drawpixel(x, y, getcolour(mpos))
            x += 1

        x, y = -width // 2, y + 1
        screen.update()

尽管它的标题,我不认为这幅画是 Mandelbrot,但它至少应该扫描 正确。

更新

既然您已经提供了 getcolour(),那么它的 Mandelbrot 性质就很清楚了。下面是完整的返工代码和一些输出:

import math
import turtle

def iterc(c):
    iters = 0
    z = 0 + 0j

    while iters < 16 and math.hypot(z.real, z.imag) < 2:
        z = z * z + c
        iters += 1

    return iters

def getcolour(pos):
    x = iterc(pos)
    colournum = int(x * 6.25)
    return 'gray{}'.format(colournum)

def drawpixel(x, y, colour):
    turtle.goto(x, y)
    turtle.dot(1, colour)

def main():
    screen = turtle.Screen()
    screen.title('Mandelbrot Set')
    screen.bgcolor('#22ccaa')

    width = int(screen.numinput('Screen size', 'What width?', 600, 100, 20000))
    height = int(screen.numinput('Screen size', 'What height?', 600, 100, 10000))
    screen.setworldcoordinates(-width // 2, -height // 2, width // 2, height // 2)
    screen.tracer(0, 0)

    radius = 2

    turtle.penup()
    turtle.hideturtle()
    turtle.speed('fastest')

    x, y = -width // 2, -height // 2
    turtle.goto(x, y)

    while y < (height // 2):

        while x < (width // 2):

            newx = x / (width // 2) * radius
            newy = y / (width // 2) * radius
            mpos = newx + newy * 1j
            drawpixel(x, y, getcolour(mpos))
            x += 1

        x, y = -width // 2, y + 1
        screen.update()

main()

OUTPUT(体积缩小,还没完成运行)

既不是最漂亮也不是最快的实现,但它是 Mandelbrot。

现在您已经看到您的代码在我的系统上运行,您需要检查您的环境(Python 版本、Windows 或 Unix 等)以了解有什么不同。以上是在 Max OSX 10.11

上使用 Python 3.6.0 完成的