简体   繁体   English

Tkinter 无法打开图像

[英]Tkinter can't open image

for some reason, Tkinter can't open my image.由于某种原因,Tkinter 无法打开我的图像。 If I don't add from tkinter import * it shows error message as:如果我不添加from tkinter import *它会显示错误消息:

Error Message without from tkinter import * :没有from tkinter import *错误消息:

C:\Users\NG>python e:/PythonTkinter/app.py
Traceback (most recent call last):
  File "e:/PythonTkinter/app.py", line 12, in <module>
    logo = Image.open('logo.png')
  File "C:\Users\NG\anaconda3\lib\site-packages\PIL\Image.py", line 2891, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'logo.png'

and if I add from tkinter import * as shown below, it shows error message as shown below.如果我from tkinter import *添加如下所示,它会显示如下所示的错误消息。

Code:代码:

import tkinter as tk
import PyPDF2
from PIL import Image, ImageTk
from tkinter import *

# begaining of our UI window
root = tk.Tk()

canvas = tk.Canvas(root, width=600, height=300)
canvas.grid(columnspan=3)

# Adding logo
# logo = ImageTk.PhotoImage(Image.open("logo.png"))
logo = Image.open("logo.png")
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image=logo)
loogo_label.image = logo
logo_label.grid(column=1, row=0)

# ending of our UI window
root.mainloop()

Error Message with from tkinter import * : from tkinter import *错误消息:

C:\Users\NG>python e:/PythonTkinter/app.py
Traceback (most recent call last):
  File "e:/PythonTkinter/app.py", line 14, in <module>
    logo = Image.open("logo.png")
AttributeError: type object 'Image' has no attribute 'open'

Image is right there in same folder where this python file is.图像就在此 python 文件所在的同一文件夹中。

在此处输入图像描述

WHAT AM I DOING WRONG?我究竟做错了什么? HELP!帮助!

Explanation:-解释:-

You should probably understand what relative path is.您可能应该了解相对路径是什么。 When relative path is used, it is not relative the location of the python file, but instead the location from where you are running the python file.使用相对路径时,它不是 python 文件的相对位置,而是运行 python 文件的位置。 Here you are running the file from:在这里,您正在从以下位置运行文件:

C:\Users\NG C:\用户\NG

But your python file and the image you are using is inside:但是您的 python 文件和您使用的图像在里面:

e:/PythonTkinter/app.py e:/PythonTkinter/app.py

Solution:-解决方案:-

So here either you can change the location from where you're running the code to the location with image file OR you can copy the image to the location you are running the py file from(ie, 'C:\Users\NG').因此,在这里,您可以将运行代码的位置更改为带有图像文件的位置,或者您可以将图像复制到运行 py 文件的位置(即“C:\Users\NG”) .


And as far as the second error is concerned, it's never a good idea to say from x import * .就第二个错误而言,说from x import *从来都不是一个好主意。 When you import '*' from tkinter , it replaces the PIL.Image with tkinter.Image .当您从tkinter导入“*”时,它会将tkinter.Image PIL.Image And hence the error.因此错误。 So either remove that line or move it to the top most.所以要么删除那条线,要么把它移到最上面。 Recommended import is:推荐的进口是:

import tkinter as tk
import PyPDF2
from PIL import Image, ImageTk

You are just using the first import here, so I don't see a point in using from tkinter import * , so just remove it.您只是在这里使用第一个导入,所以我看不到使用from tkinter import *的意义,所以只需将其删除。

You can simply do as follows您可以简单地执行以下操作

from tkinter import *

window = Tk() # instantiate an instance of a window for us
window.geometry("500x500")

#creating a photo image from png photo
icon = PhotoImage(file='GUI using Python\\flower.png')
window.iconphoto(True, icon)

window.mainloop()

starting from the comment (creating a photo image from png photo), you give a value in the file parameter the relative path not the full path, also take care of the backslash, you should write two backslashes and no need for all of these libraries that you are using unless you are counting on them for other tasks.从评论开始(从 png 照片创建照片图像),你在文件参数中给一个值相对路径而不是完整路径,还要注意反斜杠,你应该写两个反斜杠,不需要所有这些库除非您指望它们完成其他任务,否则您正在使用它。

Your PIL import should be after 'from tkinter import *'.您的 PIL 导入应该在 'from tkinter import *' 之后。 Also make sure to be in the same directory as the image还要确保与图像位于同一目录中

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

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