【问题标题】:Can't run turtle program on macOS: "turtle.TurtleGraphicsError: bad color string: blue"无法在 macOS 上运行海龟程序:\"turtle.TurtleGraphicsError: bad color string: blue\"
【发布时间】:2023-01-25 06:51:13
【问题描述】:

绘制斐波那契螺旋线的程序可以在 Windows 的虚拟机上运行,​​但在 macOS 上无法运行并且会出错。

这是我的代码:

import turtle
Import math


def fiboPlot(n):
    a = 0
    b = 1
    square_a = a
    square_b = b

    #Setting the colour of the plotting pen to blue
    x.pencolor("blue")

    # Drawing the first square
    x.forward(b * factor)
    x.left(90)
    x.forward(b * factor)
    x.left(90)
    x.forward(b * factor)
    x.left(90)
    x.forward(b * factor)

    # Proceeding in the Fibonacci Series
    temp = square_b
    square_b = square_b + square_a
    square_a = temp

    # Drawing the rest of the squares
    for i in range(1, n):
        x.backward(square_a * factor)
        x.right(90)
        x.forward(square_b * factor)
        x.left(90)
        x.forward(square_b * factor)
        x.left(90)
        x.forward(square_b * factor)

        # Proceeding in the Fibonacci Series
        temp = square_b
        square_b = square_b + square_a
        square_a = temp

    # Bringing the pen to starting point of the spiral plot
    x.penup()
    x.setposition(factor, 0)
    x.seth(0)
    x.pendown()

    # Setting the colour of the plotting pen to red
    x.pencolor("red")

    # Fibonacci Spiral Plot
    x.left(90)
    for i in range(n):
        print(b)
        fdwd = math.pi * b * factor / 2
        fdwd /= 90
        for j in range(90):
            x.forward(fdwd)
            x.left(1)
        temp = a
        a = b
        b = temp + b


# Here 'factor' signifies the multiplicative
# factor which expands or shrinks the scale
# of the plot by a certain factor.
factor = 1

# Taking Input for the number of
# Iterations our Algorithm will run
n = int(input('Enter the number of iterations (must be > 1): '))

# Plotting the Fibonacci Spiral Fractal
# and printing the corresponding Fibonacci Number
if n > 0:
    print("Fibonacci series for", n, "elements :")
    x = turtle.Turtle()
    x.speed(100)
    fiboPlot(n)
    turtle.done()
else:
    print("Numb

以下是错误:

kEventMenuPopulate to menu ''
kEventMenuPopulate to menu '<Apple>'
kEventMenuPopulate to menu 'python'
kEventMenuPopulate to menu 'File'
kEventMenuPopulate to menu 'Edit'
kEventMenuPopulate to menu 'Window'
kEventMenuPopulate to menu 'Help'
kEventMenuPopulate to menu 'Help'
kEventMenuPopulate to menu 'Help'
kEventMenuPopulate to menu 'Help'
kEventMenuPopulate to menu ''

和这个

  File "/Users/als/PycharmProjects/spiral_of_fibonacci/main.py", line 81, in <module>
    fiboPlot(n)
  File "/Users/als/PycharmProjects/spiral_of_fibonacci/main.py", line 12, in fiboPlot
    x.pencolor("blue")
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 2253, in pencolor
    color = self._colorstr(args)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 2697, in _colorstr
    return self.screen._colorstr(args)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/turtle.py", line 1159, in _colorstr
    raise TurtleGraphicsError("bad color string: %s" % str(color))
turtle.TurtleGraphicsError: bad color string: blue

在 macOS 中


在 Windows 中


我看不到代码有问题,因为它在 Windows 上运行。

【问题讨论】:

  • 这回答了你的问题了吗? Bad Color String Error in Python Using Turtle
  • 我没有看到你的颜色代码有任何问题。问题可能出在 Mac 上的底层 Tk 安装上。 "red" 等其他颜色字符串也会失败吗?解决方法可能是使用 rgb 值而不是颜色字符串,例如 x.pencolor(0,0,255)
  • 顺便说一句——您发布的代码不能按原样运行。第二行有一个拼写错误,它也在代码底部的字符串文字中间突然中断。

标签: python macos turtle-graphics python-turtle


【解决方案1】:

修复不完整的行后:

print("Numb

并更改错误行的大小写:

Import math

这段代码在我的 Mac OS (Unix) 系统上运行良好。下面是我对您的代码进行的返工以简化它,这样我就可以看到发生的一切。看看它是否更适合您:

from turtle import Screen, Turtle
from math import pi

# 'FACTOR' signifies the multiplicative factor
# which expands or shrinks the scale of the plot:
FACTOR = 2

def fiboPlot(n):
    a, b = 0, 1

    # Setting the colour of the plotting pen to blue
    turtle.pencolor('blue')

    # Drawing the first square
    for _ in range(4):
        turtle.forward(b * FACTOR)
        turtle.left(90)

    turtle.right(90)

    # Drawing the rest of the squares
    for _ in range(n - 1):
        # Proceeding in the Fibonacci Series
        a, b = b, b + a

        turtle.backward(a * FACTOR)
        turtle.right(90)
        turtle.forward(b * FACTOR)
        turtle.left(90)
        turtle.forward(b * FACTOR)
        turtle.left(90)
        turtle.forward(b * FACTOR)

    # Bringing the pen to starting point of the spiral plot
    turtle.penup()
    turtle.setposition(FACTOR, 0)
    turtle.setheading(90)
    turtle.pendown()

    # Setting the colour of the plotting pen to red
    turtle.pencolor('red')

    a, b = 0, 1

    # Fibonacci Spiral Plot
    for _ in range(n):
        print(b)
        distance = pi * b * FACTOR / 180

        for _ in range(90):
            turtle.forward(distance)
            turtle.left(1)

        a, b = b, b + a

# Input the number of iterations our algorithm will run:
n = int(input('Enter the number of iterations (must be > 1): '))

# Plot the fibonacci spiral fractal and
# print the corresponding fibonacci number:
if n > 0:
    print("Fibonacci series for", n, "elements:")
    screen = Screen()
    turtle = Turtle()
    turtle.speed('fastest')
    fiboPlot(n)
    screen.mainloop()
else:
    print("Number something or other")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-10
    • 2021-06-22
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2012-01-04
    相关资源
    最近更新 更多