为什么Python乌龟图形背景颜色没有变化?

时间:2018-10-27 15:34:47

标签: python turtle-graphics

作为Python的新手,我才刚刚开始使用图形。我刚刚看过一个教程,其中的导师使用了“ turtle”模块。尽管有我的代码,我仍在努力使背景颜色不变,标题也似乎没有改变:

#Space Invaders
import turtle
import os

#Set up screen
wn = turtle.Screen()
wn.bgcolor(33,255,0)
wn.title("Space Invaders")

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您应该已经收到错误:

turtle.TurtleGraphicsError: bad color sequence: (33, 255, 0)

Python乌龟有两种数字颜色模式,整数(0-255)和浮点(0.0-1.0)。默认情况下,它使用浮动颜色模式:

>>> import turtle
>>> turtle.colormode()
1.0
>>> help(turtle.colormode)
Help on function colormode in module turtle:

colormode(cmode=None)
    Return the colormode or set it to 1.0 or 255.

    Optional argument:
    cmode -- one of the values 1.0 or 255

    r, g, b values of colortriples have to be in range 0..cmode.

    Example:
    >>> colormode()
    1.0
    >>> colormode(255)
    >>> pencolor(240,160,80)

>>> 

您必须明确要求整数1:

# Space Invaders
import turtle

# Set up screen
wn = turtle.Screen()
wn.colormode(255)
wn.bgcolor(33, 255, 0)
wn.title("Space Invaders")

wn.mainloop()

您需要以mainloop()或其变体之一(done()exitonclick())结束,以将控制权移交给tkinter的事件循环以保持窗口打开。否则,它将脱离脚本的结尾并关闭。

相关问题