python - 关闭 matplotlib 图而不关闭 tkinter
问题描述
我在 python 2.7.12 中使用 matplotlib 和 tkinter。当使用按钮运行创建 matplotlib 图的命令时,保存图,然后用于plt.close(fig)
关闭图,tkinter 窗口也会关闭(我不想要)。如果我删除该plt.close(fig)
行,tkinter 窗口将保持打开状态,但关闭 tkinter 窗口并不会结束该过程。如何在不关闭 tkinter 窗口的情况下正确关闭 matplotlib 图?
示例代码:
import matplotlib.pyplot as plt
import tkinter as tk
def command():
fig, ax = plt.subplots(1, 1)
x = range(0, 10, 2)
y = x
ax.plot(x, y)
fig.savefig('test.png')
plt.close(fig) # this line makes the tkinter window close after the command runs
root = tk.Tk()
button = tk.Button(root, text='click me', command=command)
button.grid(row=0, column=0)
root.mainloop()
解决方案
推荐阅读
- python - Pandas Dataframe 迭代循环无限期加载
- variables - laravel中两个变量的foreach循环
- dataframe - pyspark 加入 2 个查找表
- javascript - 对变化的值做出反应而不实际改变它
- r - 如何在 tmap HTML 多边形弹出窗口或固定宽度中包装长文本字符串?tmap 可以识别换行符吗?
- c++ - 如何在 MPI 中为多个进程创建结构
- php - phpseclib rawlist() 返回 false
- apache-spark - PySpark:过滤联合的重复项,仅保留指定列的最大值的groupby行
- sql-server - 在 SQL Server 中使用透视和递归代码改进查询
- java - 贪心算法最大化分数