2017-03-01 69 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

所以,你真正的問題只是你需要交換按鈕上的圖像?你不需要兩個按鈕,你可以使用一個按鈕,只是換出圖像。 –