首页 > 解决方案 > 单击 tkinter 中的按钮后如何生成另一个图?

问题描述

我正在尝试使用 tkinter 中的入口点显示图像的不同切片。但是,当我输入一个新的切片编号时,它会在前一个切片下创建图像。我不知道如何删除第一个为第二个腾出空间。一个在此处输入图像描述

代码如下

import matplotlib
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *
import numpy.ma as ma
import cv2
from os import listdir
from os.path import isfile, join
import ellipse as el

class mclass:
    def __init__(self,  window):
        self.window = window
        self.box = Entry(window)
        self.button = Button (window, text="check", command=self.plot)
        self.box.pack ()
        self.button.pack()


    def plot (self):
        mypath='C:\\Users\\mehmet\\Desktop\\a1'
        onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
        images = np.empty(len(onlyfiles), dtype=object)
        for n in range(0, len(onlyfiles)):
          images[n] = cv2.imread( join(mypath,onlyfiles[n]),cv2.IMREAD_GRAYSCALE)

        image = np.stack([images[i] for i in range(13,299)])

        arr_size = (265,490,286)
        sphere_center = (120,238,76)
        a=11
        b=10
        c=12
        sphere = el.create_bin_sphere(arr_size,sphere_center, a,b,c)
        sphere1=255*sphere.astype(np.uint8)
        sphere2=np.swapaxes(sphere1,0,2)

        dst = cv2.bitwise_or(sphere2, image)


        img_p=dst[:,:,int(self.box.get())]

        fig = Figure(figsize=(3,3))
        a = fig.add_subplot(111)
        a.imshow(img_p,cmap='gray')
        a.plot()
        canvas = FigureCanvasTkAgg(fig, master=self.window)
        canvas.get_tk_widget().pack(side="top")
        canvas.draw()

window= Tk()
start= mclass (window)
window.mainloop()

标签: pythonnumpyuser-interfacematplotlibtkinter

解决方案


推荐阅读