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

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

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

  1. import matplotlib.backends.backend_tkagg as tkagg
  2. import matplotlib.pyplot as plt
  3. import matplotlib
  4. import tkinter as tk
  5. matplotlib.use('TkAgg')
  6. def plot_canvas(master):
  7. points = [(0, 0), (10, 0), (10, 25), (20, 25), (25, 0)]
  8. fig = plt.figure(figsize=(5, 4))
  9. fig.add_subplot(111).plot([x[0] for x in points],
  10. [x[1] for x in points],
  11. marker='.',
  12. linestyle='-')
  13. # ax.
  14. # ax.plot([10, 20], [25, 25], marker='.', linestyle=':', color='green')
  15. return tkagg.FigureCanvasTkAgg(fig, master=master).get_tk_widget()
  16. m = tk.Tk()
  17. wgt = plot_canvas(m)
  18. wgt.grid()
  19. m.mainloop()
gojuced7

gojuced71#

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

相关问题