为什么 Tkinter 使用 canvas.create_image 会中断?
我正在开发一个 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:known option "-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))))
有人能够阐明任何情况吗?
I've got a python GUI app in the workings, which I intend to use on both Windows and Mac. The documentation on Tkinter isn't the greatest, and google-fu has failed me.
In short, I'm doing:
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()
If I comment out the create_bitmap line, the app draws fine. If I comment it back in, I get the following error:
_tkinter.TclError: unknown option "-image"
Which is odd. Tkinter is fine, according to the python tests (ie, importing _tkinter, Tkinter, and doing Tk()
). I've since installed PIL against my windows setup (XP SP3, Python 2.6) imagining that it was doing some of the heavy lifting at a low level. It doesn't seem to be; I still get the aforementioned error.
The full stacktrace, excluding the code I've already pasted, is:
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))))
Anyone able to shed any light?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Tk 有两种类型的图形:位图和图像。 图像有两种形式:位图和照片。 位图和位图类型的图像不是同一件事,这会导致文档混乱。
PhotoImage 创建照片类型的图像,并且需要画布中的图像对象,因此正如您已经得出的结论,解决方案是使用 create_image。
Tk has two types of graphics, bitmap and image. Images come in two flavours, bitmap and photo. Bitmaps and Images of type bitmap are not the same thing, which leads to confusion in docs.
PhotoImage creates an image of type photo, and needs an image object in the canvas, so the solution is, as you already concluded, to use create_image.
简短回答:
当您打算使用create_image时,请勿使用create_bitmap。
Short answer:
Don't use create_bitmap when you mean to use create_image.
create_bitmap()
方法没有image
参数; 它有一个位图
参数反而。您得到的错误来自这样一个事实:在 Tkinter 中,Tcl 解释器嵌入在 Python 进程中运行,并且所有 GUI 交互都在 Python 和 Tcl 之间来回进行; 因此,您收到的错误来自 Tcl 回复“我不知道 .create_bitmap 调用中的任何 -image 选项”这一事实。
无论如何,就像 Jeff 所说,您可能需要
create_image
方法。The
create_bitmap()
method does not have animage
argument; it has abitmap
argument instead.The error you get comes from the fact that in Tkinter, a Tcl interpreter is running embedded in the Python process, and all GUI interaction goes back and forth between Python and Tcl; so, the error you get comes from the fact that Tcl replies "I don't know any -image option in the .create_bitmap call".
In any case, like Jeff said, you probably want the
create_image
method.