如何摆脱“无法释放颜色图,仍然选择调色板”错误?

我一直在处理颜色渐变的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% 的情况下不会出现任何错误,但时不时地会抛出“无法释放颜色图,仍然选择调色板”错误。


猛跑小猪
浏览 151回答 1
1回答

繁星coding

在事件驱动的环境中,我们不能简单地做while True:并期望事情有效。这样做有效地阻止了一些事件的触发。窗口关闭事件可能很棘手——比乌龟有时能够处理的更棘手,所以我们可能需要下降到 tkinter 级别才能正确执行。下面是我重新编写的代码,以使用计时器事件替换无限循环,并使用窗口关闭处理程序来捕获窗口关闭事件。处理程序尝试干净地停止您的内部循环和计时器事件,然后完成关闭窗口。加上一些其他的风格变化:from turtle import Screenfrom random import randintfrom os import systemscreen = Screen()screen.colormode(255)screen.bgcolor(0, 0, 0)curR = 0curG = 0curB = 0running = Truedef window_closing():&nbsp; &nbsp; global running&nbsp; &nbsp; running = False&nbsp; &nbsp; screen.ontimer(screen.bye, 500)def switch_color_target():&nbsp; &nbsp; global curR, curG, curB&nbsp; &nbsp; r = randint(0, 255)&nbsp; &nbsp; g = randint(0, 255)&nbsp; &nbsp; b = randint(0, 255)&nbsp; &nbsp; success = False&nbsp; &nbsp; RD = False&nbsp; &nbsp; GD = False&nbsp; &nbsp; BD = False&nbsp; &nbsp; while running and not success:&nbsp; &nbsp; &nbsp; &nbsp; if curR < r:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curR += 1&nbsp; &nbsp; &nbsp; &nbsp; elif curR > r:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curR -= 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RD = True&nbsp; &nbsp; &nbsp; &nbsp; if curG < g:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curG += 1&nbsp; &nbsp; &nbsp; &nbsp; elif curG > g:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curG -= 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GD = True&nbsp; &nbsp; &nbsp; &nbsp; if curB < b:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curB += 1&nbsp; &nbsp; &nbsp; &nbsp; elif curB > b:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; curB -= 1&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BD = True&nbsp; &nbsp; &nbsp; &nbsp; screen.bgcolor(curR, curG, curB)&nbsp; &nbsp; &nbsp; &nbsp; system("cls")&nbsp; &nbsp; &nbsp; &nbsp; print(r)&nbsp; &nbsp; &nbsp; &nbsp; print(g)&nbsp; &nbsp; &nbsp; &nbsp; print(b)&nbsp; &nbsp; &nbsp; &nbsp; success = RD and GD and BD&nbsp; &nbsp; &nbsp; &nbsp; if success:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("R")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("B")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("G")&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(curR)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(curG)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(curB)&nbsp; &nbsp; if running:&nbsp; &nbsp; &nbsp; &nbsp; 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()我和你使用的操作系统不同,所以我不能彻底测试这个——试一试看看它是否能解决你的问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python