_tkinter.TclError: couldn't recognize data in image file "background.png" when using turtle graphics

_tkinter.TclError: couldn't recognize data in image file "background.png" when using turtle graphics

我在 linux 笔记本电脑 (Ubuntu) 上写了这段代码,但是现在,当我尝试在 mac 上 运行 它时,我得到了错误:

_tkinter.TclError: couldn't recognize data in image file "background.png"

在我的图形 init 函数中尝试将图像设置为背景时会发生这种情况。

    def __init__(self):
    self.window = turtle.Screen()
    self.window.setup(724, 724)
    self.window.bgpic("background.png")
    self.window.tracer(0, 0)

Here is the image that it is using

根据https://docs.python.org/3/library/turtle.html#turtle.bgpic ,背景图片必须是 GIF。不支持 PNG。但是您可以使用 PIL/Pillow(Python 图像库)或 NumPy 将 PNG 动态转换为 GIF。