From giannicristian at msn.com Wed Jul 27 12:09:17 2016 From: giannicristian at msn.com (Gianni Iannelli) Date: Wed, 27 Jul 2016 18:09:17 +0200 Subject: [Tkinter-discuss] Image Canvas delete elements Message-ID: 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 *#----------------------------------------------------------------------class MainWindow(): #---------------- def __init__(self, main): # canvas for image self.canvas = Canvas(main, width=60, height=60) self.canvas.grid(row=0, column=0) # images self.my_images = [] self.my_images.append(PhotoImage(file = "ball1.gif")) self.my_images.append(PhotoImage(file = "ball2.gif")) self.my_images.append(PhotoImage(file = "ball3.gif")) self.my_image_number = 0 # set first image on canvas self.image_on_canvas = self.canvas.create_image(0, 0, anchor = NW, image = self.my_images[self.my_image_number]) # button to change image self.button = Button(main, text="Change", command=self.onButton) self.button.grid(row=1, column=0) #---------------- def onButton(self): # next image self.my_image_number += 1 # return to first image if self.my_image_number == len(self.my_images): self.my_image_number = 0 # change image self.canvas.itemconfig(self.image_on_canvas, image = self.my_images[self.my_image_number])#----------------------------------------------------------------------root = Tk()MainWindow(root)root.mainloop() To remove the image, I have added a new button which remove the image. Here it is: .... self.button_2 = Button(main, text="Change", command=self.onButton_2) self.button_2.grid(row=2, column=0) def onButton_2(self): #remove image self.my_images_name.remove(self.my_images_name[self.my_image_number]) # next image self.my_image_number += 1 # return to first image if self.my_image_number == len(self.my_images): 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. I've also tried to remove the canvas and add another one using these code-lines: ...#delete canvasself.canvas.delete(self.image_on_canvas)#add new canvasself.canvas = Canvas( width=60, height=60)self.canvas.grid(row=0, column=0)self.image_on_canvas = self.canvas.create_image(0, 0, anchor = NW, image = self.my_images[self.my_image_number])... Do you have any idea? I have google it but I was not able to find a solution. I have also tried to update the widget with no success. Thanks in advance!GC -------------- next part -------------- An HTML attachment was scrubbed... URL: From klappnase at web.de Fri Jul 29 05:01:46 2016 From: klappnase at web.de (Michael Lange) Date: Fri, 29 Jul 2016 11:01:46 +0200 Subject: [Tkinter-discuss] Image Canvas delete elements In-Reply-To: References: Message-ID: <20160729110146.e1784f3c62d771dde93d5d93@web.de> Hi, On Wed, 27 Jul 2016 18:09:17 +0200 Gianni Iannelli 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('', next_img) root.mainloop() ######################################## Best regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. No one wants war. -- Kirk, "Errand of Mercy", stardate 3201.7