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

C

ChrisChia

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?
 
E

Eric Brunel

ChrisChia said:
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 -
 
J

Jeff Hobbs

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?

self.label.configure(image = imagelist[1]) will change the image
displayed.
With PIL images, you can use image1.fromstring(...) that should have a
similar effect, in that it changes the underlying image data and the
label will display that.

Whether you need to label.bind or something else to effect this change
is up to you. You could have it happen on a timer, triggered by an
event, or randomly effected by solar flares, depending on your
application's needs.

Jeff
 
C

ChrisChia

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?

self.label.configure(image = imagelist[1]) will change the image
displayed.
With PIL images, you can use image1.fromstring(...) that should have a
similar effect, in that it changes the underlying image data and the
label will display that.

Whether you need to label.bind or something else to effect this change
is up to you.  You could have it happen on a timer, triggered by an
event, or randomly effected by solar flares, depending on your
application's needs.

Jeff

Yes... I don't intend to make it mutable anymore...
thanks to Jeff for the
self.label.configure

i don't even know about the configure method.
I m very new to tkinter.
Thanks everyone!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top