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

Discussion in 'Python' started by ChrisChia, Aug 16, 2010.

  1. ChrisChia

    ChrisChia Guest

    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?
    ChrisChia, Aug 16, 2010
    #1
    1. Advertising

  2. ChrisChia

    Eric Brunel Guest

    In article
    <>,
    ChrisChia <> 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 -
    Eric Brunel, Aug 16, 2010
    #2
    1. Advertising

  3. ChrisChia

    Jeff Hobbs Guest

    On Aug 16, 7:30 am, ChrisChia <> 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?


    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
    Jeff Hobbs, Aug 16, 2010
    #3
  4. ChrisChia

    ChrisChia Guest

    On Aug 17, 2:57 am, Jeff Hobbs <> wrote:
    > On Aug 16, 7:30 am, ChrisChia <> 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?

    >
    > 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!!!
    ChrisChia, Aug 17, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steven
    Replies:
    0
    Views:
    419
    steven
    Nov 10, 2004
  2. Dirk Schumann

    Cannot draw on mutable image

    Dirk Schumann, Dec 27, 2004, in forum: Java
    Replies:
    0
    Views:
    425
    Dirk Schumann
    Dec 27, 2004
  3. Replies:
    5
    Views:
    1,869
    Andrew Thompson
    Jan 12, 2005
  4. christopher diggins

    How bad is making a field mutable?

    christopher diggins, Jun 3, 2005, in forum: C++
    Replies:
    6
    Views:
    457
    christopher diggins
    Jun 3, 2005
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    106
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page