How to solve the error "AttributeError: type object 'Image' has no attribute 'open'" in python?

How to solve the error "AttributeError: type object 'Image' has no attribute 'open'" in python?

我试图在 Tkinter canvas 中显示图像以及一些文本,但我 运行 遇到以下错误。此外,当 运行 在 anaconda 中使用 Spyder 时,我的 mac 不显示按钮的背景颜色(Spyder 是最新的)。

我的python代码是:

from tkinter import *  
from PIL import ImageTk,Image  

def plot_best_batsmen():
    best_batsmen = dataset.loc[dataset.loc[dataset['Innings']>=15,'Average'].idxmax(),'Names']
    message = ("The best Batsman of the Tournament could possibly be: ", best_batsmen)
    canvas_width = 500
    canvas_height = 500
    root = Tk()
    root.geometry("600x600")
    root.title("New Window")
    canvas = Canvas(root, width=canvas_width, height=canvas_height)
    canvas.create_text(1, 10, anchor=W, text=message)
    img = ImageTk.PhotoImage(Image.open("prediction.jpg"))
    canvas.create_image(20, 20, anchor=NW, image=img)
    canvas.image = img
    canvas.pack()
    root.mainloop()

在运行ning:

时显示错误信息如下
Exception in Tkinter callback
Traceback (most recent call last):
  File "/anaconda3/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Users/vivekchowdary/Documents/PS3_Final_Project/Batsmen.py", line 110, in plot_best_batsmen
    canvas.create_image(20, 20, anchor=NW, image=img)
  File "/anaconda3/lib/python3.7/tkinter/__init__.py", line 2489, in create_image
    return self._create('image', args, kw)
  File "/anaconda3/lib/python3.7/tkinter/__init__.py", line 2480, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: image "pyimage3" doesn't exist

按钮代码如下:

b1 = Button(root, text="Elbow Method", command=plot_elbow, bg="green", fg="white").pack(side = LEFT)
b2 = Button(root, text="K-Means Clustering", command=plot_kmeans, bg="blue", fg="white").pack(side = LEFT)
b3 = Button(root, text="Batsmen who scored 4 or more Hundreds", command=plot_hundreds, bg="#D35400", fg="white").pack(side = LEFT)
b4 = Button(root, text="Runs Scored by Various Players", command=plot_runs, bg="#117A65", fg="white").pack(side = LEFT)
b5 = Button(root, text="Best Batsmen", command=plot_best_batsmen, bg="#34495E", fg="white").pack(side = LEFT)
b6 = Button(root, text="Stop", command=root.destroy, bg="red", fg="white").pack(side = BOTTOM)

我想让Tkinter 显示下图。但它报告了一个错误。谁能帮我解决这个错误?

看来这个错误与你的tkinter无关。要确定问题,请问

from PIL import Image 
print(Image.open)

如果您已正确安装所有内容,结果应如下所示。

< function open at 0x0000000008A26488 >

tkinter 还有一个 class/function 叫做 Image。您还从 PIL 导入了图像。您需要确定您要使用哪个 Image.open。 tkinter.Image 没有属性 'open'。

我自己找到了答案,只是想分享它来帮助有同样疑问的人。

我们需要在 Tkinter 之后导入 PIL,因为 Tkinter 也有自己的 class 图像,但没有 'open' 功能,如果在 PIL 之后导入,来自 Tkinter 的图像将替换来自 PIL 的图像。

必须做的第二件事是用 root = Toplevel() 替换 root = Tk() 因为问题是 Python/Tkinter 试图从按钮构建 canvas ,它本质上是试图在 root 下创建两个 windows 并摔倒。

现在,代码正在将照片导入 GUI 并且工作正常。

所以,下面的代码终于可以正常工作了: def plot_best_batsmen(): best_batsmen = dataset.loc[dataset.loc[dataset['Innings']>=15,'Average'].idxmax(),'Names'] message = ("The best Batsman of the Tournament could possibly be: ",best_batsmen) canvas_width = 400 canvas_height = 500 root = Toplevel() root.geometry("700x600") root.title("New Window") canvas = Canvas(root, width=canvas_width, height=canvas_height) canvas.create_text(1, 10, anchor=W, text=message) img = ImageTk.PhotoImage(Image.open("virat.jpg")) canvas.create_image(150, 20, anchor=NW, image=img) canvas.image = img canvas.pack() root.mainloop()