[Tkinter-discuss] Image Canvas delete elements

Michael Lange klappnase at web.de
Fri Jul 29 05:01:46 EDT 2016


Hi,

On Wed, 27 Jul 2016 18:09:17 +0200
Gianni Iannelli <giannicristian at msn.com> wrote:

> Dear All,
> I'm playing a little bit with Tkinter, and I have started playing on
> Image slider. I would like to create a simple window which change the
> image based on a simple button. Furthermore, if the 'remove' button is
> clicked, the actual image is removed and never show up again. I was
> able to found a very nice working example, but I'm not able to develop
> the 'remove' button. I copy and paste a code that I have found on the
> web: from Tkinter import
(...)
> # #self.my_image_number = 0        # change image
> # #self.canvas.itemconfig(self.image_on_canvas, image = self.my_images
> # #[self.my_image_number]).... The image is actually removed when I
> # #print the list of images but, unfortunately, it keeps appearing on
> # #my canvas.

If you want to change the currently displayed image, the easiest way
should be to leave the Canvas alone and instead simply configure the
PhotoImage object to use a different image file, as in this primitive
example:

########################################
from Tkinter import *

root = Tk()
c = Canvas(root)
c.pack(fill='both', expand=1)
images = ['a.gif', 'b.gif']
img = PhotoImage(file=images[0])
c.create_image(20, 20, image=img)

def next_img(event):
    current = img.cget('file')
    i = images.index(current)
    if i == len(images) - 1:
        new_i = 0
    else:
        new_i = i + 1
    img.configure(file=images[new_i])

root.bind('<F1>', next_img)

root.mainloop()
########################################

Best regards

Michael	 	   		  


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

No one wants war.
		-- Kirk, "Errand of Mercy", stardate 3201.7


More information about the Tkinter-discuss mailing list