繁体   English   中英

为什么 Python 海龟图形背景颜色没有改变?

[英]Why isn't Python turtle graphics background color changing?

作为 Python 的新手,我才刚刚开始使用图形。 我刚刚看了一个教程,其中导师使用了“海龟”模块。 尽管我的代码,但我的背景颜色和标题似乎都没有改变,我正在挣扎:

#Space Invaders
import turtle
import os

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

任何帮助表示赞赏!

您应该收到以下错误:

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)

>>> 

您必须明确请求整数一:

# 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()exitonclick() ,以将控制权移交给 tkinter 的事件循环以保持窗口打开。 否则它将从脚本的末尾脱落并关闭。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM