Python TKInter:AttributeError:'NoneType'对象没有属性'create_image'

时间:2013-02-14 16:16:49

标签: python tkinter

我正在尝试创建一个Tkinter GUI,并且在尝试将gif图像放在画布上时出现属性错误。

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'

提前致谢。

1 个答案:

答案 0 :(得分:4)

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

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

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