python tkinter 无法显示图片
fromTkinterimport*root=Tk()canvas=Canvas(root)#lab=Label(root,bg='red')icon=r'C:\User...
from Tkinter import *
root = Tk()
canvas = Canvas(root)
#lab = Label(root,bg='red')
icon =r'C:\Users\zhx\Pictures\11.jpg'
gif1 = PhotoImage(icon)
canvas.create_image(0, 0, image = gif1)
#lab.config(image = icon)
canvas.pack()
#lab.pack()
root.mainloop()
用canvas和label都试了一下总是报错
>>>
Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "E:\python\w\test1.py", line 17, in <module>
canvas.create_image(0, 0, image = gif1)
File "D:\python27\Lib\lib-tk\Tkinter.py", line 2260, in create_image
return self._create('image', args, kw)
File "D:\python27\Lib\lib-tk\Tkinter.py", line 2251, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: image "C:\Users\zhx\Pictures\11.jpg" doesn't exist
可是这个文件是一定存在的,我确认了多次,,,网上部分资料说是python把生成的图像当垃圾回收了,可总得有办法显示把,要不这个属性还有啥用,望大神指点 展开
root = Tk()
canvas = Canvas(root)
#lab = Label(root,bg='red')
icon =r'C:\Users\zhx\Pictures\11.jpg'
gif1 = PhotoImage(icon)
canvas.create_image(0, 0, image = gif1)
#lab.config(image = icon)
canvas.pack()
#lab.pack()
root.mainloop()
用canvas和label都试了一下总是报错
>>>
Traceback (most recent call last):
File "<string>", line 254, in run_nodebug
File "E:\python\w\test1.py", line 17, in <module>
canvas.create_image(0, 0, image = gif1)
File "D:\python27\Lib\lib-tk\Tkinter.py", line 2260, in create_image
return self._create('image', args, kw)
File "D:\python27\Lib\lib-tk\Tkinter.py", line 2251, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: image "C:\Users\zhx\Pictures\11.jpg" doesn't exist
可是这个文件是一定存在的,我确认了多次,,,网上部分资料说是python把生成的图像当垃圾回收了,可总得有办法显示把,要不这个属性还有啥用,望大神指点 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询