我在Python的Tkinter中编写了一个GUI,我找不到如何使用canvas的create_image方法只从spritesheet中绘制一个sprite.提前感谢任何能告诉我我需要做什么的人!
我一直试图在我的Tkinter小部件中包含图像,但似乎没有任何效果.这是我的代码:
from Tkinter import *
from PIL import Image
root = Tk()
image = Image.open('images/myimage.jpg')
root.image = image
b = Radiobutton(root, text='Image',image=image,value='I')
b.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:Trac
eback (most recent call last):
File "C:/Users/.../loadimages.py", line 7, in <module>
b = Radiobutton(root, text='Image',image=image,value='I')
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2714, in __init__
Widget.__init__(self, master, 'radiobutton', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1974, in __init__
(widgetName, self._w) + extra + self._options(cnf))
TclError: image "<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=402x439 at 0x2A6B080>" doesn't exist
Run Code Online (Sandbox Code Playgroud)
大多数互联网消息来源建议保留对图像的引用以避免垃圾收集器,但我不知道我怎么能比我在这里做的更多.还有关于有多个Tk实例的建议,但我只有一个.
对谁有帮助,提前谢谢!