2017-03-01 41 views
0

我正在使用Tkinter来显示图像。在Python中完全删除图像3

有没有办法从屏幕和内存中完全删除这些图像?我们现在删除的方式会删除图像,但不会影响内存。

谢谢。

+0

您在询问之前是否做过任何研究?这个网站上有很多关于回收记忆的问题。例如:http://stackoverflow.com/q/3935675/7432 –

回答

0

感谢您的链接,我现在正在阅读它。图像最终的解决方案是在需要时简单地将两个图像放在另一个的上面。这似乎(我希望)不会造成电子内存泄漏,因为相同的物品会不断重复使用:

from tkinter import * 
#by David Beck www.dwbeck.com 

green_circle = 'Green_Circle.png' 
red_circle = 'Red_Circle.png' 



def create_image(): 
    if var.get() == 1: 
     red_button.lift(green_button) 
    if var.get() == 0: 
     green_button.lift(red_button) 


def pass_pass(): 
    pass 


root=Tk() 

var = BooleanVar() 

w = Canvas(width=250, height=150) 
w.pack() 

green_circle_tk = PhotoImage(file=green_circle) 
red_circle_tk = PhotoImage(file=red_circle) 



green_button = Button(root, text="", command=pass_pass) 
green_button.config(image=red_circle_tk) 
green_button.place(x=20, y=100) 
red_button = Button(root, text="", command=pass_pass) 
red_button.config(image=green_circle_tk) #,width="40",height="40" 
red_button.place(x=20, y=100) 


checkbox_2 = Checkbutton(root, text='red/green', variable=var, command=create_image).pack() 


root.mainloop() 
+0

所以,你真正的问题只是你需要交换按钮上的图像?你不需要两个按钮,你可以使用一个按钮,只是换出图像。 –