Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我只是想在GUI中显示一个图像。我做错了什么?_Python_Python 3.x_Image_Tkinter - Fatal编程技术网

Python 我只是想在GUI中显示一个图像。我做错了什么?

Python 我只是想在GUI中显示一个图像。我做错了什么?,python,python-3.x,image,tkinter,Python,Python 3.x,Image,Tkinter,我试图在GUI中显示一个图像,但不明白出了什么问题。我不断地发现这个错误: AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo' 我的代码(特别是带有my_img=…的行)应该是什么样子 我的代码: from tkinter import * from PIL import ImageTk,Image my_img = ImageTk.PhotoImage(Image.open("iu.j

我试图在GUI中显示一个图像,但不明白出了什么问题。我不断地发现这个错误:

AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
我的代码(特别是带有
my_img=…
的行)应该是什么样子

我的代码:

from tkinter import *
from PIL import ImageTk,Image

my_img = ImageTk.PhotoImage(Image.open("iu.jpeg"))
my_label = Label(image=my_img)
my_label.pack()


root = Tk()
root.title("ICON PRACTICE")
root.iconbitmap('iu.ico')

button_quit = Button(root, text = "EXIT", command=root.quit)
button_quit.pack()
root.mainloop()
完全错误

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    my_img = ImageTk.PhotoImage(Image.open("test.png"))
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 112, in __init__
    self.__photo = tkinter.PhotoImage(**kw)
  File "/usr/lib/python3.8/tkinter/__init__.py", line 4064, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/usr/lib/python3.8/tkinter/__init__.py", line 3997, in __init__
    raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x7f7148fadc10>
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 118, in __del__
    name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'

回溯(最近一次呼叫最后一次):
文件“main.py”,第4行,在
my_img=ImageTk.PhotoImage(Image.open(“test.png”))
文件“/opt/virtualenvs/python3/lib/python3.8/site packages/PIL/ImageTk.py”,第112行,在__
自拍照=tkinter.PhotoImage(**kw)
文件“/usr/lib/python3.8/tkinter/_init__.py”,第4064行,在_init中__
图像。_u初始(自我,“照片”,名称,cnf,主机,**千瓦)
文件“/usr/lib/python3.8/tkinter/_init__.py”,第3997行,在_init中__
raise RUNTIMERROR('创建映像太早')
RuntimeError:创建映像太早
在中忽略异常:
回溯(最近一次呼叫最后一次):
文件“/opt/virtualenvs/python3/lib/python3.8/site packages/PIL/ImageTk.py”,第118行,在__
name=self.\u photo.name
AttributeError:“PhotoImage”对象没有属性“\u PhotoImage\u photo”

尝试这样做,可能是因为您在打开图像并创建照片对象后创建了根对象

import os
from tkinter import *
from PIL import ImageTk,Image

root= Tk()
i = Image.open("C:/path/to/the/image/directory/image.png") 
photo = ImageTk.PhotoImage(i)

root.mainloop()

只有在声明了
root=Tk()
Tk()
启动底层Tcl解释器)之后,
Label
小部件才会工作。然后,所有子窗口小部件必须将root作为其第一个参数(例如,
标签(root,text='hi')
)。您在尝试使用解释器后启动了它,因此Python引发了一个异常。

为什么不将图像的完整路径名传递给
image.open()
?将当前目录更改为映像目录可能会影响程序的其他部分。导入语句:
import image
import ImageTk
from PIL import Tk
也不正确。如果路径有问题,OP可能会得到不同的错误。您应该放置三行(在导入语句之后)在创建
root
实例之后,即在
按钮退出=…
之前。在尝试创建图像之前,您是否尝试过移动根窗口的创建?另外,请告诉我们是哪一行创建了错误。我们可以猜测,但如果不必这样做会更好。这是否回答了您的问题?