python - 单击 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()
解决方案
推荐阅读
- flutter - Flutter 运行卡在 Preparing “Install Android SDK Build-Tools 28.0.3 (revision: 28.0.3)”
- android - 片段未在后台堆栈中注册
- css - 为什么具有绝对位置的内联元素的行为不像块级元素?
- python - 在 django 中仅打印第一个返回 HttpResponse
- python - 从字幕文件中删除不在单词列表中的单词(常用单词)
- swiftui - 如何在 SwiftUI 中读取和过滤大型 Realm 数据集?
- asp.net-core - 在 PostAsJsonAsync 上接收空响应
- java - Java LWJGL OpenGL 3 将 3d 点转换为 2d 点
- powershell - Powershell 不返回哈希表中的键值
- mongoose - 如何根据其他 fieldName mongooseSchema 定义 slug