其他折叠内的Python图像

2024-04-23 09:20:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python天气程序,使用openweathermap.org API。主文件位于一个目录中,但我要在Tkinter窗口中的图像位于image/目录中。目前,我已经将图片放入python脚本的文件夹中,但是里面有很多图片,所以看起来很乱。我通过以下方式将图像调用到窗口上:

imageURL = str(icon + ".gif")
photo = PhotoImage(file=imageURL)
w = Label(window, image=photo)
w.photo = photo

但当我尝试使用:

^{pr2}$

它只返回错误:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 531, in callit
func(*args)
File "H:\Weather statioN\weather.py", line 55, in init
photo = PhotoImage(file=imageURL)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3306, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 3262, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't open "09d.gif": no such file or directory

有办法吗?在


Tags: inpyimageselfinittkinterlibline
1条回答
网友
1楼 · 发布于 2024-04-23 09:20:34

回溯提到您仍然使用icon + '.gif'-

self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't open "09d.gif": no such file or directory

检查是否遗漏了添加代码,或是遗漏了将先前测试中的图像复制到images目录中。在

另外,建议使用os.path库使用当前操作系统的路径分隔符自动将目录与路径连接起来,这将使脚本独立于平台。在

^{pr2}$

您可以使用上面的代码来处理您的案例。在

相关问题 更多 >