我尝试在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()
1条答案
按热度按时间gojuced71#
有两种解决方案:
1.将
matplotlib.use('TkAgg')
更改为matplotlib.use('Agg')
1.使用
matplotlib.figure.Figure(...)
代替plt.figure(...)