【问题标题】:_tkinter.TclError: couldn't recognize data in image file "background.png" when using turtle graphics_tkinter.TclError:使用海龟图形时无法识别图像文件“background.png”中的数据
【发布时间】:2017-12-15 15:45:14
【问题描述】:

我在 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

【问题讨论】:

  • 我在基于 Ubuntu 16 的 Linux Mint 18.2 上的 Python 2.7.12 上使用 bgpic() 测试了您的图像,它可以工作。 turtle 是基于 tkinter 构建的,它默认只支持 gif,所以在 turtle 中也可能有问题。模块PIL/pillow 中有一个特殊的类可以将png(和其他格式)与tkinter 一起使用。我已经安装了pillow,如果没有pillow,我无法检查bgpic 是否有问题。你可以尝试安装pillow

标签: python python-2.7 turtle-graphics


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 2012-05-17
    • 2020-05-11
    • 2014-03-18
    相关资源
    最近更新 更多