Closing main window doesn't stop tkinter app process

59 Views Asked by At

I try to implement matplotlib chart into tkinter app and it works but after closing main window (m) the process is still going and cant stop it without closing the console. Why?

import matplotlib.backends.backend_tkagg as tkagg
import matplotlib.pyplot as plt
import matplotlib
import tkinter as tk
matplotlib.use('TkAgg')


def plot_canvas(master):
    points = [(0, 0), (10, 0), (10, 25), (20, 25), (25, 0)]
    fig = plt.figure(figsize=(5, 4))
    fig.add_subplot(111).plot([x[0] for x in points],
                              [x[1] for x in points],
                              marker='.',
                              linestyle='-')

    # ax.
    # ax.plot([10, 20], [25, 25], marker='.', linestyle=':', color='green')
    return tkagg.FigureCanvasTkAgg(fig, master=master).get_tk_widget()


m = tk.Tk()
wgt = plot_canvas(m)
wgt.grid()
m.mainloop()
1

There are 1 best solutions below

0
acw1668 On

There are two solutions:

  1. change matplotlib.use('TkAgg') to matplotlib.use('Agg')

  2. use matplotlib.figure.Figure(...) instead of plt.figure(...)