我试图用Tkinter制作一个简单的GUI,它使用Matplotlib生成大量绘图并将它们保存到硬盘上。在
附件是一个简单的代码,但是在保存了所有绘图之后,Tkinter GUI关闭,脚本停止。我想这个问题可能与plt.关闭(),因为当我删除plt.关闭(),GUI窗口不再关闭,但毫不奇怪,内存很快就会被填满,直到整个程序崩溃。在
而不是plt.关闭(),我尝试使用plt.clf公司(),plt.gcf公司().clear(),图清除()他们都没用。它们使GUI窗口保持不变,但会导致内存问题。在
有人知道为什么吗plt.关闭()关闭Tkinter GUI窗口,如何阻止它?我需要图形用户界面(GUI)留下来,并在我处理完对象后,将它们从内存中删除。在
我使用的是Python3.6.3rc1、windows7、Tkinter 8.6和matplotlib3.0.2。在from tkinter import *
import matplotlib.pyplot as plt
import os
def make_plot():
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
for j in range(0,20):
fig = plt.figure(num=None, figsize=(20, 10), dpi=200, facecolor='w', edgecolor='w')
plt.plot(x,y)
plt.xlabel("x")
plt.ylabel("y")
out_name = os.getcwd()+ "\\" + str(j)+".png"
print(out_name)
plt.savefig(out_name)
plt.close()
class Application(Frame):
def run_make_plot(self):
make_plot()
def createWidgets(self):
self.button = Button(self)
self.button["text"] = "Plot"
self.button["command"] = self.run_make_plot
self.button.pack()
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()