我在下面的代码中看到了内存泄漏。我在单独的模块中创建和处理我的数据,但这不会导致我所看到的泄漏。我相信这是因为每次更改比例时我都会调用绘图类的新实例,尽管我不确定如何解决此问题。我已经阅读了这个线程,但是当我尝试self.canvas.destroy()
在我的代码上实现方法时,我收到一个错误。我想知道什么方法可以应用于下面的代码来解决我的问题?
代码片段:
from Tkinter import *
class Interface_On:
def Interface_Elements(self, master):
self.master=master
self.master.title( "My Canvas")
self.c=Canvas(self.master, width=1000, height=1000, bg='black')
self.c.grid(row=0, column=0)
menubar = Menu(master)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=self.Edit_New)
menubar.add_cascade(label="File", menu=filemenu)
master.config(menu=menubar)
drawing_utility_run=Drawing_Utility()
drawing_utility_run.drawer(self.c)
def Edit_New(self):
Export_Poscar = self.Export_Poscar = Toplevel(self.master)
self.Export_Poscar.title('New Ribbon...')
self.Export_Poscar.geometry('300x400')
self.scale_Label= Label(Export_Poscar, width=15, text='scale:')
self.scale_Label.grid(row=2, column=0)
self.scale_Label= Label(Export_Poscar, width=15, text='scale:')
scale_var = StringVar()
self.scale_Spin= Spinbox(Export_Poscar, from_=1, to=1000, increment=1, width=15, command=self.execute_request_immediate, textvariable=scale_var)
self.scale_Spin.grid(row=2, column=2)
def execute_request_immediate(self):
global scale
User_Set_Scale=float(self.scale_Spin.get())
scale=User_Set_Scale
drawing_utility_run=Drawing_Utility()
drawing_utility_run.drawer(self.c)
class Drawing_Utility:
def drawer(self, canvas):
self.canvas=canvas
self.canvas.delete('all')
import Generator #generates my data (imports 'scale' from above where possible)
Generator_run=Generator.Generator()
Generator_run.generator_go()
from Generator import coordinates_x_comp, coordinates_y_comp #imports necessary lists
import Processor #Imports coordinates_x_comp, coordinates_y_comp, cleans and analyses
Process_xy_Data=Processor.Data_Processor()
Process_xy_Data.Process_Data()
from Processor import p_1, p_2
for Line in xrange(len(p_1)):
self.canvas.create_line(p_1[Line],p_2[Line], fill='red', activefill='blue', width=1)
root=Tk()
run_it_canvas=Interface_On()
run_it_canvas.Interface_Elements(root)
root.mainloop()