我目前正在开发一个tkinter(GUI)项目,它可以接收用户的平均值并将其返回给他们。我希望使用PIL库在父窗口中显示图像。昨天这个库可以很好地工作,并在目录中定位我的图像,但是今天它似乎找不到目录了,有人可以帮助我并指导我吗?我不知道为什么今天PIL库出了问题,通常它都可以正常工作。(我已经尝试重新安装文件但没有效果!)。
这是我的代码
import tkinter
from PIL import Image, ImageTk
root = tkinter.Tk();
def showImg():
load = Image.open('Desktop\example.jpg')
render = ImageTk.PhotoImage(load)
img = tkinter.Label(root, image = render)
img.image = render
img.pack()
button = tkinter.Button(root, text='Click me to see an image.', command=showImg).pack();
root.title('Imaging test');
root.geometry('450x450');
root.mainloop();
这是我的错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
return self.func(*args)
File "C:\Users\Pamal\Desktop\Documents\Python Folder\Python [Learn]\imaging_example.py", line 7, in showImg
load = Image.open('Desktop\example.jpg')
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2219, in open
fp = builtins.open(fp, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Desktop\\example.jpg
对我来说,文本 FileNotFoundError: [Errno 2] No such file or directory: 'Desktop\example.jpg' 没有意义。就在昨天,当我使用PIL库时,我能够正常显示图像而没有收到此错误消息,但今天我只得到了这个错误消息。我甚至尝试指定文件路径到C:\,但它不接受。请有人帮帮我,我需要在我的tkinter(GUI)父窗口中获取图像,但这个库不允许。 (附言:我已经尝试重新安装PIL,但没有改变一点,如果您知道任何[易于使用]成像库,请告诉我)。
你应该知道:
- 我正在运行Python 3.4.2
- Windows 8.1机器
- 我不是Python专家,所以请不要给我展示一些复杂的代码。
下面是我最近收到的错误的图像。我为图像提供了特定位置,但仍然收到这个烦人的错误。还有人解释为什么会有两个[//]斜杠,即使我只输入了一个。这是否影响我的文件位置?我用黄色突出显示它们,抱歉,我只是想引起你的注意,也是我新来的堆栈溢出,所以哎呀: P
它是否与这行有关:
r'C:\Users\Pamal\Desktop\example.jpg'
- Malik Brahimi