matplotlib 关闭主窗口不会停止tkinter应用程序进程

ne5o7dgx  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(117)

我尝试在tkinter应用程序中实现matplotlib图表,它工作正常,但在关闭主窗口(m)后,该过程仍在进行,并且无法在不关闭控制台的情况下停止它。为什么?

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()
gojuced7

gojuced71#

有两种解决方案:
1.将matplotlib.use('TkAgg')更改为matplotlib.use('Agg')
1.使用matplotlib.figure.Figure(...)代替plt.figure(...)

相关问题