Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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 如何在Tkinter中获取matplotlib子地块上的按钮?_Python_Matplotlib_Tkinter_Widget_Tkinter Canvas - Fatal编程技术网

Python 如何在Tkinter中获取matplotlib子地块上的按钮?

Python 如何在Tkinter中获取matplotlib子地块上的按钮?,python,matplotlib,tkinter,widget,tkinter-canvas,Python,Matplotlib,Tkinter,Widget,Tkinter Canvas,我是Tkinter的新手,希望在matplotlib子批次中获得按钮,并使用Tkinter显示它。我确实在网上搜索过,但找不到完全符合我需要的东西。如果有人能提供任何见解,那真是太好了 到目前为止,我尝试了类似的方法(参见下面的代码),并在图形画布中获得了matplotlib图形。我想知道如何在每个子批次中启用两个按钮 import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1 import ImageGrid import nump

我是Tkinter的新手,希望在matplotlib子批次中获得按钮,并使用Tkinter显示它。我确实在网上搜索过,但找不到完全符合我需要的东西。如果有人能提供任何见解,那真是太好了

到目前为止,我尝试了类似的方法(参见下面的代码),并在图形画布中获得了matplotlib图形。我想知道如何在每个子批次中启用两个按钮

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
import numpy as np
from tkinter import *
import matplotlib
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure 
from PIL import Image

win = Tk()

folder = r'D:\V\Bilder_resized\Test'
im = np.arange(100)
im.shape = 10, 10

fig = plt.figure(figsize = (10,10))

grid = ImageGrid(fig, 221,  # similar to subplot(111)
             nrows_ncols=(2, 2),  # creates 2x2 grid of axes
             axes_pad=0.25,  # pad between axes in inch.
             )
for gridax in grid:
    gridax.set_title('True Positive', fontsize = 13)
    gridax.set_xticks([])
    gridax.set_yticks([])
grid_1 = ImageGrid(fig, 222,  # similar to subplot(111)
             nrows_ncols=(1, 2),  # creates 2x2 grid of axes
             axes_pad=0.25,  # pad between axes in inch.
             )
for gridax in grid_1:
    gridax.set_title('False Positive', fontsize = 13)
    gridax.set_xticks([])
    gridax.set_yticks([])
grid_2 = ImageGrid(fig, 223,  # similar to subplot(111)
             nrows_ncols=(1, 2),  # creates 2x2 grid of axes
             axes_pad=0.25,  # pad between axes in inch.
             )
for gridax in grid_2:
    gridax.set_title('False Negative', fontsize = 13)
    gridax.set_xticks([])
    gridax.set_yticks([])
grid_3 = ImageGrid(fig, 224,  # similar to subplot(111)
             nrows_ncols=(2, 2),  # creates 2x2 grid of axes
             axes_pad=0.25,  # pad between axes in inch.
             )
for gridax in grid_3:
    gridax.set_title('True Negative', fontsize = 13)
    gridax.set_xticks([])
    gridax.set_yticks([])

for i in range(4):
    image = os.path.join(folder, true_positive[i])
    img= Image.open(image)
    d = ImageDraw.Draw(img)
    d.text((30,30),"{}".format(np.round(true_positive_confidence[i],2)), 
fill = (255,255,0))
grid[i].imshow(img) 
# The AxesGrid object work as a list of axes.
x = len(false_positive)
c = len(false_negative)

if x < 2:
    a =x 
else:
   a =2

for i in range(a):
    image = os.path.join(folder, false_positive[i])
    img= Image.open(image)
    d = ImageDraw.Draw(img)
    d.text((30,30),"{}".format(np.round(false_positive_confidence[i],2)), 
fill = (255,255,0))
    grid_1[i].imshow(img)
if c < 2:
    b = c
else:
    b =2    
for i in range(b):
    image = os.path.join(folder, false_negative[i])
    img= Image.open(image)
    d = ImageDraw.Draw(img)
    d.text((30,30),"{}".format(np.round(false_negative_confidence[i],2)), 
fill = (255,255,0))
    grid_2[i].imshow(img)
for i in range(4):
    image = os.path.join(folder, true_negative[i])
    img= Image.open(image)
   d = ImageDraw.Draw(img)
d.text((30,30),"{}".format(np.round(true_negative_confidence[i],2)), fill = (255,255,0))
grid_3[i].imshow(img)

canvas = FigureCanvasTkAgg(fig, master=win)

graph_widget = canvas.get_tk_widget()
graph_widget.grid(row=0, column=0, columnspan=3, sticky = 'nsew')
win.mainloop()
plt.show()
导入matplotlib.pyplot作为plt
从mpl_toolkits.axes_grid1导入ImageGrid
将numpy作为np导入
从tkinter进口*
导入matplotlib
从matplotlib.backends.backend_tkagg导入图CAVASTKAGG
从matplotlib.figure导入图形
从PIL导入图像
win=Tk()
folder=r'D:\V\Bilder\u resized\Test'
im=np.arange(100)
im.shape=10,10
图=plt.图(图尺寸=(10,10))
网格=图像网格(图221,类似于子地块(111)
nrows_ncols=(2,2),#创建2x2轴网格
轴#垫=0.25,#轴之间的垫(英寸)。
)
对于网格中的gridax:
gridax.set_title('True-Positive',fontsize=13)
gridax.set_xticks([])
gridax.set_yticks([])
网格1=图像网格(图222,类似于子图111)
nrows_ncols=(1,2),#创建2x2轴网格
轴#垫=0.25,#轴之间的垫(英寸)。
)
对于网格_1中的gridax:
gridax.set_title('False-Positive',fontsize=13)
gridax.set_xticks([])
gridax.set_yticks([])
网格2=图像网格(图223,类似于子图111)
nrows_ncols=(1,2),#创建2x2轴网格
轴#垫=0.25,#轴之间的垫(英寸)。
)
对于grid_2中的gridax:
gridax.set_title('False Negative',fontsize=13)
gridax.set_xticks([])
gridax.set_yticks([])
网格3=图像网格(图224,类似于子图111)
nrows_ncols=(2,2),#创建2x2轴网格
轴#垫=0.25,#轴之间的垫(英寸)。
)
对于网格3中的gridax:
gridax.set_title('True-Negative',fontsize=13)
gridax.set_xticks([])
gridax.set_yticks([])
对于范围(4)中的i:
image=os.path.join(文件夹,真\正[i])
img=Image.open(图像)
d=图像绘制。绘制(img)
d、 文本((30,30),“{}”格式(np.round(真正置信度[i],2)),
填充=(255255,0))
网格[i].imshow(img)
#AxesGrid对象作为轴列表工作。
x=len(假阳性)
c=len(假阴性)
如果x<2:
a=x
其他:
a=2
对于范围(a)中的i:
image=os.path.join(文件夹,假阳性[i])
img=Image.open(图像)
d=图像绘制。绘制(img)
d、 文本((30,30),“{}”格式(np.round(假阳性置信度[i],2)),
填充=(255255,0))
网格_1[i].imshow(img)
如果c<2:
b=c
其他:
b=2
对于范围(b)中的i:
image=os.path.join(文件夹,假阴性[i])
img=Image.open(图像)
d=图像绘制。绘制(img)
d、 文本((30,30),“{}”,
填充=(255255,0))
网格_2[i].imshow(img)
对于范围(4)中的i:
image=os.path.join(文件夹,真\负[i])
img=Image.open(图像)
d=图像绘制。绘制(img)
d、 文本((30,30),“{}”.format(np.round(真负置信度[i],2)),fill=(255255,0))
网格_3[i].imshow(img)
canvas=FigureCanvasTkAgg(fig,master=win)
graph\u widget=canvas.get\u tk\u widget()
graph_widget.grid(行=0,列=0,列span=3,粘性='nsew')
win.mainloop()
plt.show()
代码的输出:

我想在每个子窗口下面嵌入“上一个”和“下一个”按钮,以便在文件夹中导航以显示上一组或下一组图像。 我还添加了一个关于我如何需要它的示例图像。


如果有人能提供一些关于如何做到这一点的想法,那将非常有帮助。对不起,如果我的帖子不清楚

更好的组织方式是创建一个
框架
来容纳每个
图形
对象和按钮,然后将框架网格化。下面是一个使用从
tk.Frame
继承的类的示例:

import numpy as np
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure

win = tk.Tk()

class FigureFrame(tk.Frame):
    def __init__(self,master=None,**kwargs):
        super().__init__(master,**kwargs)
        self.fig = Figure(figsize=(5, 4), dpi=100)
        self.canvas = FigureCanvasTkAgg(self.fig, master=self)
        graph_widget = self.canvas.get_tk_widget()
        graph_widget.grid(row=0, column=0, columnspan=3, sticky = 'nsew')
        self.plot_next()
        tk.Button(self,text="Prev",command=self.plot_next).grid(row=1,column=0,sticky="w")
        tk.Button(self,text="Next",command=self.plot_next).grid(row=1,column=2,sticky="e")

    def plot_next(self):
        self.fig.clear()
        r = np.random.randint(1,10,5)
        self.fig.add_subplot(111).plot(r)
        self.canvas.draw_idle()

for i in range(4):
    f = FigureFrame(win)
    f.grid(row=i//2,column=i%2)

win.mainloop()

哇。。这是我想要的一个很好的例子。非常感谢:)嗨。我确实创建了不同的框架,并使用网格将它们连接起来。但是,有没有为每一帧而不是整个窗口指定标题??在
plot\u next
函数中,
subplot=self.fig.add\u subplot(111);子地块(r);子地块。设置标题(“图形标题”)
。接下来,请作为一个新问题提问。