简体   繁体   English

如何在GUI中使用Tkinter显示图像

[英]How to display an image using tkinter in gui

The given code is not working, as it gives the error of file not found for 'haridwar.jpg' though I tried putting it in in Python35 and also on desktop. 给定的代码不起作用,因为它给出了找不到“ haridwar.jpg”文件的错误,尽管我尝试将其放在Python35和台式机中。 Please help 请帮忙

import tkinter as tk  
from PIL import Image,ImageTk  
root = tk.Tk()  
root.title("display image")  
im=Image.open("haridwar.jpg")  
photo=ImageTk.PhotoImage(im)  
cv = tk.Canvas()  
cv.pack(side='top', fill='both', expand='yes')  
cv.create_image(10, 10, image=photo, anchor='nw')  
root.mainloop()

Judging by your question you might not have the file in a good location; 根据您的问题判断,您可能没有将该文件放在合适的位置; also you are not providing a path to those locations. 您也没有提供通往这些位置的路径。 So lets break it down a little. 因此,让我们分解一下。

You said you placed the image in the Python35 folder. 您说过将图像放置在Python35文件夹中。 Without knowing more I would imagine you are talking about the python default directory located somewhere like C:\\program files\\Python35 . 不知道更多信息,我想您正在谈论的是python默认目录,位于C:\\program files\\Python35

If this is the case then change the line: 如果是这种情况,请更改该行:

im=Image.open("haridwar.jpg") 

To this: 对此:

im=Image.open("C:\program files\Python35\haridwar.jpg")

though this is not a good place for your image. 虽然这不是您图像的好地方。 We will get to that in a sec. 我们将在几秒钟内解决这个问题。

As you stated you also tried your desktop. 如您所说,您还尝试了台式机。 So you would want to provide a path to your desktop. 因此,您需要提供一个通往桌面的路径。

Something like this: 像这样:

im=Image.open("C:/Users/your_user_folder/Desktop/haridwar.jpg")

This is also not a great place for your file. 这也不是存放文件的好地方。

Lets try something else. 让我们尝试其他事情。 Lets put the file inside your working python directory. 让我们将文件放入您的工作python目录中。

For example if your main.py file is located inside of 例如,如果您的main.py文件位于

"C:/myworkspace/my_program/main.py"

then you can place that image in the same my_program folder and your code should work as is. 那么您可以将该图像放置在同一my_program文件夹中,并且您的代码应该可以正常工作。

If you want to have a folder just for images you could have one in a directory that looks like this: 如果您想拥有一个仅用于图像的文件夹,则可以在如下所示的目录中拥有一个文件夹:

"C:/myworkspace/my_program/my_images/haridwar.jpg"

In this case you can provide a short path like this: 在这种情况下,您可以提供以下简短路径:

im=Image.open("./my_images/haridwar.jpg") 

notice the . 注意. before the /my_image folder. /my_image文件夹之前。 This is used to tell Python it can look inside its current working directory for the folder. 这是用来告诉Python,它可以在文件夹的当前工作目录中查找。

I tried your code using a directory/filename i know was correct, and it works. 我使用我知道正确的目录/文件名尝试了您的代码,并且可以正常工作。 You have an error in the spelling of your directory/filename or you got the directory wrong. 您的目录/文件名的拼写错误或目录错误。

Make sure you have the directory and file name correct. 确保目录和文件名正确。

For example, I have "Image.jpg" on my desktop 例如,我的桌面上有“ Image.jpg”

import tkinter as tk  
from PIL import Image,ImageTk  
root = tk.Tk()  
root.title("display image")  
im=Image.open("C:/Users/<myname>/Desktop/Image.jpg")  #This is the correct location and spelling for my image location
photo=ImageTk.PhotoImage(im)  
cv = tk.Canvas()  
cv.pack(side='top', fill='both', expand='yes')  
cv.create_image(10, 10, image=photo, anchor='nw')  
root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM