我有一个 python GUI 应用程序正在开发中,我打算在 Windows 和 Mac 上使用它。 Tkinter 上的文档不是最好的,google-fu 也让我失望了。
简而言之,我在做:
c = Canvas(
master=frame,
width=settings.WINDOW_SIZE[0],
height=settings.WINDOW_SIZE[1],
background=settings.CANVAS_COLOUR
)
file = PhotoImage(file=os.path.join('path', 'to', 'gif'))
c.create_bitmap(position, image=file)
c.pack()
root.mainloop()
如果我注释掉 create_bitmap 行,应用程序可以正常绘制。如果我重新评论它,我会收到以下错误:
_tkinter.TclError: 未知选项“-image”
这很奇怪。根据 python 测试(即导入 _tkinter、Tkinter 并执行 Tk()
),Tkinter 很好。从那以后,我针对我的 Windows 设置(XP SP3、Python 2.6)安装了 PIL,想象它在低级别上做了一些繁重的工作。似乎不是;我仍然遇到上述错误。
完整的堆栈跟踪,不包括我已经粘贴的代码,是:
File "C:\Python26\lib\lib-tk\Tkinter.py", line 2153, in create_bitmap
return self._create('bitmap', args, kw)
File "C:\Python26\lib\lib-tk\Tkinter.py", line 2147, in _create
*(args + self._options(cnf, kw))))
任何人都能够阐明什么?
最佳答案
Tk有两种图形,位图和图像。图像有两种风格,位图和照片。位图和位图类型的图像不是一回事,这会导致文档困惑。 PhotoImage 创建类型为 photo 的图像,并且在 Canvas 中需要一个图像对象,因此正如您已经得出的结论,解决方案是使用 create_image。
关于python - 为什么 Tkinter 会破坏使用 canvas.create_image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/824988/