如何摆脱 'Unable to free colormap, pallette is still selected' 错误?
How to get rid of 'Unable to free colormap, pallette is still selected' error?
我一直在处理颜色渐变的 python 脚本中的错误,但在关闭 python 控制台时出现这个模糊的错误,上面写着:
Unable to free colormap, pallette is still selected
然后,我得到一个弹出窗口说 "Python has stopped responding"。我认为这意味着它崩溃了,但我不知道。我不知道为什么会这样,但到目前为止它似乎是随机的。
我过去尝试过许多不同版本的 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% 的情况下不会出现任何错误,但时不时地,它会抛出 "Unable to free colormap, pallette is still selected" 错误。
在事件驱动的环境中,我们不能简单地做 while True:
并期望一切正常。这样做可以有效地阻止某些事件的触发。 window 关闭事件可能很棘手 -- 有时 turtle 无法处理,因此我们可能需要下降到 tkinter 级别才能正确完成它。
下面是我对您的代码进行的修改,使用计时器事件替换无限循环,并使用 window 关闭处理程序来捕获 window 关闭事件。处理程序尝试干净地停止您的内部循环和计时器事件,然后完成关闭 window。再加上一些其他的风格变化:
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()
我使用的操作系统与您不同,因此我无法对此进行全面测试 -- 试试看是否能解决您的问题。
我一直在处理颜色渐变的 python 脚本中的错误,但在关闭 python 控制台时出现这个模糊的错误,上面写着:
Unable to free colormap, pallette is still selected
然后,我得到一个弹出窗口说 "Python has stopped responding"。我认为这意味着它崩溃了,但我不知道。我不知道为什么会这样,但到目前为止它似乎是随机的。
我过去尝试过许多不同版本的 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% 的情况下不会出现任何错误,但时不时地,它会抛出 "Unable to free colormap, pallette is still selected" 错误。
在事件驱动的环境中,我们不能简单地做 while True:
并期望一切正常。这样做可以有效地阻止某些事件的触发。 window 关闭事件可能很棘手 -- 有时 turtle 无法处理,因此我们可能需要下降到 tkinter 级别才能正确完成它。
下面是我对您的代码进行的修改,使用计时器事件替换无限循环,并使用 window 关闭处理程序来捕获 window 关闭事件。处理程序尝试干净地停止您的内部循环和计时器事件,然后完成关闭 window。再加上一些其他的风格变化:
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()
我使用的操作系统与您不同,因此我无法对此进行全面测试 -- 试试看是否能解决您的问题。