首页 > 解决方案 > 如何摆脱“无法释放颜色图,仍选择调色板”错误?

问题描述

我一直在使用 python 脚本处理颜色渐变的错误,但是在关闭 python 控制台时出现这个模糊的错误:

Unable to free colormap, pallette is still selected

然后,我收到一个弹出窗口,说“Python 已停止响应”。我认为这意味着它崩溃了,但我不知道。我不知道它为什么会发生,但到目前为止它似乎是随机的。

过去,我尝试过许多不同版本的 if 语句、数学和执行,但没有任何方法可以解决它。

import turtle, random, os
turtle.colormode(255)
turtle.bgcolor(0, 0, 0)
curX = 0
curY = 0
curZ = 0
while True:
    x = random.randint(0, 255)
    y = random.randint(0, 255)
    z = random.randint(0, 255)
    success = False
    XD = 0
    YD = 0
    ZD = 0
    while success == False:
        if curX < x:
            curX = curX + 1
        elif curX > x:
            curX = curX - 1

        if curY < y:
            curY = curY + 1
        elif curY > y:
            curY = curY - 1

        if curZ < z:
            curZ = curZ + 1
        elif curZ > z:
            curZ = curZ - 1

        turtle.bgcolor(curX, curY, curZ)
        os.system("cls")
        print(x),
        print(y),
        print(z)
        print(curX),
        print(curY),
        print(curZ)
        if curX == x:
            print("X")
            XD = 1
        if curY == y:
            print("Y")
            YD = 1
        if curZ == z:
            print("Z")
            ZD = 1

        if XD + YD + ZD == 3:
            success = True

当我关闭程序时,我希望它在 100% 的时间里没有任何错误地关闭,但时不时地,它会抛出“无法释放颜色图,仍然选择调色板”错误。

标签: pythonturtle-graphics

解决方案


在事件驱动的环境中,我们不能简单地做事while True:并期望事情能够奏效。这样做有效地阻止了某些事件的触发。窗口关闭事件可能很棘手 - 比 turtle 有时能够处理的还要多,所以我们可能需要下拉到 tkinter 级别才能正确执行此操作。

下面是我对您的代码的修改,以使用计时器事件来替换无限循环,并使用窗口关闭处理程序来捕获窗口关闭事件。处理程序尝试干净地停止您的内部循环和计时器事件,然后完成关闭窗口。加上一些其他的风格变化:

from turtle import Screen
from random import randint
from os import system

screen = Screen()
screen.colormode(255)
screen.bgcolor(0, 0, 0)

curR = 0
curG = 0
curB = 0

running = True

def window_closing():
    global running
    running = False
    screen.ontimer(screen.bye, 500)

def switch_color_target():
    global curR, curG, curB

    r = randint(0, 255)
    g = randint(0, 255)
    b = randint(0, 255)

    success = False

    RD = False
    GD = False
    BD = False

    while running and not success:
        if curR < r:
            curR += 1
        elif curR > r:
            curR -= 1
        else:
            RD = True

        if curG < g:
            curG += 1
        elif curG > g:
            curG -= 1
        else:
            GD = True

        if curB < b:
            curB += 1
        elif curB > b:
            curB -= 1
        else:
            BD = True

        screen.bgcolor(curR, curG, curB)

        system("cls")

        print(r)
        print(g)
        print(b)

        success = RD and GD and BD

        if success:
            print("R")
            print("B")
            print("G")
        else:
            print(curR)
            print(curG)
            print(curB)

    if running:
        screen.ontimer(switch_color_target, 250)

switch_color_target()

canvas = screen.getcanvas()
root = canvas.winfo_toplevel()
root.protocol("WM_DELETE_WINDOW", window_closing)

screen.mainloop()

我没有使用与您相同的操作系统,因此我无法对此进行彻底测试-尝试一下它是否可以解决您的问题。


推荐阅读