TkPhotoImage (click and change image button)

Discussion in 'Ruby' started by csjasnoch@wisc.edu, Aug 26, 2005.

  1. Guest

    So I found this simple code for using a button with a picture..

    require 'tk'



    def doit

    puts "Want to change image and set some flags here....\n"
    end


    class Picture


    def initialize(root)
    image1 = TkPhotoImage.new { file "light_on.gif" }
    image2 = TkPhotoImage.new { file "light_off2.gif" }
    b = TkButton.new(root) {
    image image1
    command proc {self.doit} }
    b.bind("Enter") { b.configure('image'=>image2) }
    b.bind("Leave") { b.configure('image'=>image1) }
    b.pack('side'=>'left', 'padx'=>10, 'pady'=>10)
    end
    end



    parent = TkRoot.new{ title 'Pictures' }
    Picture.new(parent)
    Tk.mainloop

    I dont really care about the Enter/Leave imaging. But rather I want it
    to change image when it is clicked...

    Is there something to pass it (like "Clicked" ) that would bind when it
    is clicked?

    But ideally I want to be able to check a current status variable and
    depending on the variable image becomes one or the other. It is for a
    light switch (thus the files are named "light_on" etc).
     
    , Aug 26, 2005
    #1
    1. Advertising

  2. Guest

    For future reference the line:
    command proc {self.doit} }
    should be
    command proc {doit} }
     
    , Aug 26, 2005
    #2
    1. Advertising

  3. Guest

    Seems making readers for the button will do it. If anyone has a simpler
    way I would like to know.. I will modify this (for my application), but
    here it is if anyone else ever needs to know


    require 'tk'






    class Picture
    attr_reader :b, :image1, :image2, :satus
    attr_writer :b, :image1, :image2, :status

    def doit

    puts "Something done....\n"
    if(@status) #turn off
    @status = false
    @b.configure('image' => @image2)
    else #turn on
    @status = true
    @b.configure('image' => @image1)
    end
    end
    def initialize(root)
    #init status variable
    @status = true
    @image1 = TkPhotoImage.new { file "light_on.gif" }
    @image2 = TkPhotoImage.new { file "light_off2.gif" }

    changeImage = proc{doit}

    @b = TkButton.new(root) {
    image @image1
    command changeImage }
    #@b.bind("Enter") { @b.configure('image'=>@image2) } #of course no
    need to bind upon entrance and leaving...
    #@b.bind("Leave") { @b.configure('image'=>@image1) }
    @b.pack('side'=>'left', 'padx'=>10, 'pady'=>10)
    end
    end



    parent = TkRoot.new{ title 'Pictures' }
    Picture.new(parent)
    Tk.mainloop
     
    , Aug 26, 2005
    #3
    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. Purvi T
    Replies:
    0
    Views:
    900
    Purvi T
    Oct 19, 2004
  2. Amy
    Replies:
    0
    Views:
    1,015
  3. Ara.T.Howard

    TkPhotoImage :data=>buf

    Ara.T.Howard, Feb 21, 2004, in forum: Ruby
    Replies:
    2
    Views:
    184
    Ara.T.Howard
    Feb 24, 2004
  4. email55555 email55555

    [Tk] TkPhotoImage question

    email55555 email55555, Dec 26, 2004, in forum: Ruby
    Replies:
    1
    Views:
    145
    Hidetoshi NAGAI
    Dec 26, 2004
  5. Ed Redman

    using TkPhotoImage

    Ed Redman, Oct 20, 2007, in forum: Ruby
    Replies:
    1
    Views:
    143
    Hidetoshi NAGAI
    Oct 21, 2007
Loading...

Share This Page