使用Python Turtle绘制圆点时触发turtle.Terminator错误求助
解决turtle库绘制圆点时的
turtle.Terminator错误 错误原因分析
- 窗口提前终止:
screen.exitonclick()会阻塞程序直到用户关闭窗口,窗口关闭后turtle的绘图上下文已销毁,后续调用turtle.dot()必然触发终止错误。 - colormode参数错误:
turtle.colormode("rgb")写法不正确,需传入数值255来启用0-255范围的RGB颜色值。 - Turtle实例化错误:
tim = turtle.Turtle缺少括号,正确实例化应为tim = turtle.Turtle()(你代码里未用到该实例,可移除或修正)。
修正后的代码
import turtle import random color_list = [(225, 156, 75), (33, 95, 140), (127, 184, 208), (222, 207, 106), (25, 52, 73), (223, 78, 51), (165, 21, 43), (142, 98, 42), (183, 47, 80), (125, 196, 140), (206, 129, 158), (37, 134, 48), (103, 11, 55), (206, 91, 106), (139, 178, 46), (146, 207, 222), (54, 54, 84), (76, 31, 23), (28, 156, 170), (9, 79, 116), (83, 73, 40), (227, 181, 160), (220, 174, 183), (160, 208, 177), (86, 159, 111), (107, 123, 156)] # 初始化绘图环境 turtle.colormode(255) screen = turtle.Screen() # 绘制圆点 turtle.dot(90, random.choice(color_list)) # 最后等待窗口关闭 screen.exitonclick()
关键修正点
- 将
screen.exitonclick()移至绘图代码之后,确保绘图完成后再等待窗口关闭。 - 把
turtle.colormode("rgb")改为turtle.colormode(255),匹配你使用的0-255范围RGB元组。 - 变量名
list改为color_list,避免与Python内置类型list重名。
内容的提问来源于stack exchange,提问作者YAKOBISAAC
相关产品推荐
相关产品推荐

