das
das

Reputation: 679

Tkinter canvas refreshing

I'm creating a frame and showing an image in the frame using canvas in Tkinter. But I need to show images continuously one after another in a loop. But couldn't refresh the canvas. Below is my code.

cwgt=Canvas(self.parent.Frame1)
cwgt.pack(expand=True, fill=BOTH)
image1 = Image.open(image1)
image1 = ImageTk.PhotoImage(image1)
cwgt.img=image1
cwgt.create_image(0, 0, anchor=NW, image=image1)
cwgt.delete("all")

The cwgt.delete("all") doesn't work.

Upvotes: 0

Views: 2821

Answers (1)

Billal BEGUERADJ
Billal BEGUERADJ

Reputation: 22744

The cwgt.delete("all") doesn't work.

Well, not only that line does not work, but nothing else works, so I am showing you here a minimal running example based on your text (not your code) to explain you how to achieve that.

The delete() method performs what you want to do. You can pass it the string all as argument to delete all items present on your Tkinter.Canvas widget, or specify a reference to the item you want to clear away.

Full program

'''
Created on May 2, 2016

@author: Billal Begueradj
'''
import Tkinter as Tk
from PIL import Image, ImageTk

class Begueradj(Tk.Frame):
    '''
    Dislay an image on Tkinter.Canvas and delete it on button click
    '''
    def __init__(self, parent):
        '''
        Inititialize the GUI with a button and a Canvas objects
        '''
        Tk.Frame.__init__(self, parent)
        self.parent=parent
        self.initialize_user_interface()
   
    def initialize_user_interface(self):
        """
        Draw the GUI
        """
        self.parent.title("Billal BEGUERADJ: Image deletion")       
        self.parent.grid_rowconfigure(0,weight=1)
        self.parent.grid_columnconfigure(0,weight=1)
        self.parent.config(background="lavender")    
        
        # Create a button and append it  a callback method to clear the image          
        self.deleteb = Tk.Button(self.parent, text = 'Delete', command = self.delete_image)
        self.deleteb.grid(row = 0, column = 0)
        
        self.canvas = Tk.Canvas(self.parent, width = 265, height = 200)  
        self.canvas.grid(row = 1, column = 0)   
           
        # Read an image from my Desktop
        self.image = Image.open("/home/hacker/Desktop/homer.jpg")
        self.photo = ImageTk.PhotoImage(self.image)        
        # Create the image on the Canvas     
        self.canvas.create_image(132,100, image = self.photo)
        
    def delete_image(self):
        '''
        Callback method to delete image
        '''
        self.canvas.delete("all")  
        

# Main method
def main():
    root=Tk.Tk()
    d=Begueradj(root)
    root.mainloop()

# Main program       
if __name__=="__main__":
    main()

In case you have more than one element on your Tkinter.Canvas widget and you want to delete only your image, you can specify its id to the delete() method because the Tkinter.Canvas.create_image() returns the id of the image created (this is not mentioned in the documentation I linked to though).

This means, in the above code you can run:

self.ref_id = self.canvas.create_image(132,100, image = self.photo)

and inside delete_image() method:

self.canvas.delete(self.ref_id) 

Demo

This is what you get:

enter image description here

After clicking on the button, the image will be cleared away:

enter image description here

Upvotes: 1

Related Questions