[Tkinter-discuss] refreshing images

Mark Peterson markpete at gmx.com
Wed May 5 19:35:30 CEST 2010


Hi, all.  I'm brand new to using Tkinter, and was hoping to get a tip
on getting images to refresh after pressing a button.  The complete
code I'm using is below.

Basically, when I click on the "Next image" button, it will delete the
original image (good) but the next image doesn't show up (bad).  Is
there a command that gets the canvas to redraw with the second image?

Since I'm very new to Tkinter, the more explicit you could be about
how to change the code, the more helpful it would be.  Thanks very
much for any help.

Best,
   Mark

def nextImage():

    global canvas
    global item

    canvas.delete(item)

    im = Image.open("image2.gif")
    photo = ImageTk.PhotoImage(im)
    item = canvas.create_image(10,10,anchor=NW, image=photo)


from Tkinter import *
import Image
import ImageTk

root = Tk()

im = Image.open("image1.gif")
canvas = Canvas(root, height=im.size[1]+20, width=im.size[0]+20)
canvas.pack(side=LEFT,fill=BOTH,expand=1)

photo = ImageTk.PhotoImage(im)
item = canvas.create_image(10,10,anchor=NW, image=photo)

b1 = Button(root, text="Next image", width=15, command=nextImage)
b1.pack()

mainloop()


More information about the Tkinter-discuss mailing list