Python TKInter : AttributeError: 'NoneType' object has no attribute 'create_image'

标签 python tkinter

我正在尝试创建 Tkinter GUI,但在尝试将 gif 图像放在 Canvas 上时出现属性错误。

canvas_1 = Canvas(width = 800, height = 450, bg = "blue").pack()
gif = PhotoImage(file = "C:\\Users\\Luke\\Desktop\\fb.gif")
canvas_1.create_image(0, 0, image = gif, anchor = NW)

这是我得到的错误
  canvas_1.create_image(0, 0, image = gif, anchor = NW)
AttributeError: 'NoneType' object has no attribute 'create_image'

提前致谢。

最佳答案

Tkinter ,方法.pack.grid返回 None .他们不返回 Widget .

修复很简单。将其拆分为 2 行:

canvas_1 = Canvas(width = 800, height = 450, bg = "blue")
canvas_1.pack()

关于Python TKInter : AttributeError: 'NoneType' object has no attribute 'create_image' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14879205/

相关文章:

Python 将 lambda 参数指定为函数参数

python - 如何使用 Tkinter 和函数找到哪个复选按钮被选中?

javascript - 以精确数量获取项目的算法

python - Hadoop流式传输调用python脚本

python 3 : Integer not repeated

python - tkinter Canvas 保持最小化

python - 带有透明图像的可拖动 Tkinter 标签仍然覆盖父 Canvas 中的图像

python - Twitter 流数据中的时间格式

python - 带有图像的标签周围有白色边框?

python - Tkinter:获取 Canvas 属性和选项值