Search code examples
pythonmatplotlibtkintertoplevel

How to properly close tkinter Toplevel with matplotlib embeded?


I'm making a GUI application and I want to use matplotlib to display some plots. I have structured my code like that:

import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
import matplotlib.pyplot as plt


class PopUP_Graph(tk.Toplevel):
    def __init__(self, *args, **kargs):
        super().__init__(*args, **kargs)
        self.fig, self.ax = plt.subplots()
        self.drawing_area = FigureCanvasTkAgg(self.fig, master=self)
        self.drawing_area.draw()
        self.toolbar = NavigationToolbar2Tk(self.drawing_area, self)
        self.toolbar.update()
        self.drawing_area.get_tk_widget().pack(side="top", fill="both", expand=1)


class App:
    def __init__(self, root):
        self.master = root
        self.button = tk.Button(root, text="Open graph", command=self.open_graph)
        self.button.grid(column=0, row=0)

    def open_graph(self):
        popupgraph1 = PopUP_Graph(self.master)
        popupgraph1.mainloop()


w, h = 900, 600
root = tk.Tk()
root.geometry(f'{w}x{h}+0+0')
app = App(root)
root.mainloop()

The problem I have is that when the toplevel window is displayed and then closed, the program won't close correctly even after closing the root window.

Is there a way to fix this??


Solution

  • As Henry Yik pointed out matplotlib.figure.Figure should be used instead of pyplot. Now it works:

    import tkinter as tk
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
    from matplotlib.figure import Figure
    
    
    class PopUP_Graph(tk.Toplevel):
        def __init__(self, *args, **kargs):
            super().__init__(*args, **kargs)
            # self.fig, self.ax = plt.subplots()
            self.fig = Figure()
            self.ax = self.fig.add_subplot()
            self.drawing_area = FigureCanvasTkAgg(self.fig, master=self)
            self.drawing_area.draw()
            self.toolbar = NavigationToolbar2Tk(self.drawing_area, self)
            self.toolbar.update()
            self.drawing_area.get_tk_widget().pack(side="top", fill="both", expand=1)
    
    
    class App:
        def __init__(self, root):
            self.master = root
            self.button = tk.Button(root, text="Open graph", command=self.open_graph)
            self.button.grid(column=0, row=0)
    
        def open_graph(self):
            popupgraph1 = PopUP_Graph(self.master)
            popupgraph1.mainloop()
    
    
    w, h = 900, 600
    root = tk.Tk()
    root.geometry(f'{w}x{h}+0+0')
    app = App(root)
    root.mainloop()