Python 无法在TKinter中显示图像

Python 无法在TKinter中显示图像,python,numpy,tkinter,Python,Numpy,Tkinter,我从我的项目中得到了以下简化代码。我的错误在于线路 self.displayimg = tk.Button(self.frame, text = 'Display', width = 25, command = lambda: show) spyder给我的错误消息是没有定义名称“show”,即使它是我在类中明确定义的函数 from PIL import Image, ImageTk import matplotlib.pyplot as plt x = 100 y = 100 xtmi =

我从我的项目中得到了以下简化代码。我的错误在于线路

self.displayimg = tk.Button(self.frame, text = 'Display', width = 25, command = lambda: show)
spyder给我的错误消息是没有定义名称“show”,即使它是我在类中明确定义的函数

from PIL import Image, ImageTk
import matplotlib.pyplot as plt
x = 100
y = 100
xtmi = 200
xtma = 205 
xs = 1024
ytmi = 500
ytma = 505
ys = 768
xarr = np.zeros(x)
yarr = np.zeros(y)
output = np.meshgrid(xarr,yarr)
output=output[0]
def mkPIL(array):
    im = Image.fromarray(np.uint8(array))
    return im
im = mkPIL(output)
plt.imshow(im, cmap='gray')


import tkinter as tk
from tkinter import Canvas, Label


root = tk.Tk()

root.mainloop()


class Manipulation:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.frame.pack()
        self.kill = tk.Button(self.frame, text = 'Kill', width = 25, command = self.close)
        self.kill.pack()
        self.displayimg = tk.Button(self.frame, text = 'Display', width = 25, command = lambda: show)
        self.displayimg.pack()
    def close(self): 
        self.master.destroy()
    def show(): 
        img = ImageTk.PhotoImage(im)
        panel = Label(root, image=im)
        panel.image = img
        panel.place(x=0,y=0)

对图像的引用永远不会保存。您可以在函数中创建它,而不是作为类属性创建。此映像在函数完成后进行垃圾回收。若要修复此问题,请将该函数转换为方法,并将保存的图像转换为类属性。“名称‘show’未定义”:应为
command=self.show
def show(self):
请共享整个错误消息。
panel=Label(root,image=im)
应为
panel=Label(root,image=img)
也一样。感谢您的回复,我的代码现在可以使用了。我只需要正确地定位图像。