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
_______________________________________________
Tkinter-discuss mailing list
[email protected]
https://mail.python.org/mailman/listinfo/tkinter-discuss