python turtle colormode无法正常工作

时间:2016-04-27 02:15:59

标签: python turtle-graphics

from turtle import *
import time
ht()
setup(width=500, height=500, startx=0, starty=0)
x=0
y=0
goto(0, 0)
colormode(255)
while True:
    write("Please type your screens size in pixels into the console.", move=False, align="center", font=("Arial", 10, "normal"))
    x = int(input('x'))
    y = int(input('y'))
    if x > 0:
        print('.')
    if y > 0:
        print('...')
        break
    if x==0:
        x=1000
        y=500
        break
    else:
        clear()
        write("Please enter a valid number (ie. x, y)", move=False, align="center", font=("Arial", 28, "normal"))    
setup(width=1400, height=800, startx=100, starty=20)
clear()
def FADE_IN_OUT(arg, align, font, size, Norm, fspeedin, fspeedout, pause):
    r=255
    g=255
    b=255
    for i in range(100):
        pencolor((r,g,b))
        write(arg, move=False, align=align, font=(font, size, Norm))
        r-=25.5
        g-=25.5
        b-=25.5
        time.sleep(fspeedin/100)
        clear()
        time.sleep(pause)
    for i in range(100):
        pencolor((r,g,b))
        write(arg, move=False, align=align, font=(font, size, Norm))
        r+=25.5
        g+=25.5
        b+=25.5
        time.sleep(fspeedout/100)
        clear()
FADE_IN_OUT("47 Studios", "center", "Arial", x/5, "normal", 2.5, 2.5, 5)
#the following is the error message I receive:
Traceback (most recent call last):
  File "C:/Python34/Survive.py", line 48, in <module>
    FADE_IN_OUT("47 Studios", "center", "Arial", x/5, "normal", 2.5, 2.5, 5)
  File "C:/Python34/Survive.py", line 33, in FADE_IN_OUT
    write(arg, move=False, align=align, font=(font, size, Norm))
  File "<string>", line 1, in write
  File "C:\Python34\lib\turtle.py", line 3431, in write
    end = self._write(str(arg), align.lower(), font)
  File "C:\Python34\lib\turtle.py", line 3403, in _write
    self._pencolor)
  File "C:\Python34\lib\turtle.py", line 597, in _write
    fill = pencolor, font = font)
  File "<string>", line 1, in create_text
  File "C:\Python34\lib\tkinter\__init__.py", line 2342, in create_text
    return self._create('text', args, kw)
  File "C:\Python34\lib\tkinter\__init__.py", line 2318, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: expected integer but got "200.0"

我正在尝试使文本淡入淡出,但它所做的只是提供有关超出范围的错误消息。我被告知使用colormode(255),但它不起作用。我不确定255是高还是高,但请帮忙。

1 个答案:

答案 0 :(得分:0)

我测试了你的代码,它在使用Turtle 0.0.2的Python 2.7.10环境中工作正常。

我认为你的问题是你在x/5中传递了FADE_IN_OUT参数,你必须使用python 3.你得到的错误,

_tkinter.TclError: expected integer but got "200.0"

抱怨接收浮点而不是整数。尝试使用Python 3的整数除法运算符//而不是单个/

例如,像这样呼叫FADE_IN_OUT

 FADE_IN_OUT("47 Studios", "center", "Arial", x//5, "normal", 2.5, 2.5, 5)

或者,尝试在Python 2中运行代码。