0

I'm using Tkinter to display images.

Is there a way to completely delete these images from the screen and memory? The way we are deleting at the moment deletes the image but not it's footprint in the ram.

Thanks.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
David W. Beck
  • 182
  • 1
  • 9
  • Have you done any research before asking? There are many questions on this site related to reclaiming memory. For example: http://stackoverflow.com/q/3935675/7432 – Bryan Oakley Mar 01 '17 at 13:33

1 Answers1

0

Thanks for the link, I'm reading it now. The solution with the images in the end was to simply have both and 'lift' one above the other when needed. This seems (I hope) to not create e memory leak as the same items are constantly reused:

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()
David W. Beck
  • 182
  • 1
  • 9
  • so, your real problem was simply that you needed to swap images on a button? You don't need two buttons for that, you can use one button and just swap out the image. – Bryan Oakley Mar 02 '17 at 18:39