how to switch image in tkinter? making it mutable? how?

Eric Brunel eric.brunel at pragmadev.nospam.com
Mon Aug 16 11:27:17 EDT 2010


In article 
<414ff6dd-73ef-48df-bd2b-080a2c710e5b at h17g2000pri.googlegroups.com>,
 ChrisChia <chrischia82 at gmail.com> wrote:

> I have this:
> image1 = ImageTk.PhotoImage(file = "c:\\f1.jpg")
> image2 = ImageTk.PhotoImage(file = "c:\\f2.jpg")
> 
> imagelist.append(image1)
> imagelist.append(image2)
> 
> self.label  = tk.Label(image = imagelist[0])
> 
> is there a way that i can create a method to switch the display the
> image2 (imagelist 2nd element)
> without using label.bind?
> 
> can i make an image mutable?

Unfortunately, I don't know the package ImageTk you're using. But at tk 
level, images are mutable without problem. It seems however that the tcl 
commands allowing to read an existing image from a file are not exposed 
at Tkinter level. But it is still possible to send the command to the 
underlying tcl interpreter to do it:

from Tkinter import *
root = Tk()
image = PhotoImage(file='f1.gif')
label = Label(root, image=image)
## When you want to change the image:
root.tk.call(image, 'read', 'f2.gif', '-shrink')

This will only work with image formats supported in the tcl/tk core, 
which are basically only GIF so far.

BTW, I don't understand why you talk about label.bindŠ If you need to do 
anything when the label is clicked, you have to make a binding on the 
label whatever it is.

HTH anyway.
 - Eric -



More information about the Python-list mailing list