Not signed in (Sign In)

Categories


 

Vanilla 1.1.10 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorhpl
    • CommentTimeMay 5th 2007
     
    Hello everybody!
    This is my first Post on this forum... before explaining my problem, I wanna thank Selsine for his blog!

    Here are my problem with the pyGtk library:

    I have made a table for my window... table = gtk.Table(ROWS, COLS)
    and I have attached into a table-cell an image...
    img = gtk.Image()
    img.set_from_file("image.jpg")
    table.attach(img, x, x1, y, y1)
    and this is ok...

    Now I want to change this image on the table-cell by a callback function (e.g. clicked button...)

    I have tried to find solution on the documentation and google... but I have been unable to solve.

    What I have to do?
    Do you have another idea about change an image in the window by a callback function?

    Thanks!
    ^__^
    • CommentAuthorselsine
    • CommentTimeMay 5th 2007
     
    Thanks for your kind words!

    Now when (or in response to what signal or user interaction) do you want to change the image? Do you want to change it when the user clicks on the table? (maybe try connecting the table to the button_press_event signal?) Or when the user clicks on the image (again maybe look at the button_press_event or button_press_event)

    So maybe try something like:


    def button_press_callback(widget, event, data ):
    print "clicked on" % widget

    table.connect("button_press_event", button_press_callback)
    img.connect("button_press_event", button_press_callback)


    And check to see if you are getting the button_press_event being sent to the button_press_callback.

    And if it's working you could then just set the image in the button_press_callback.

    If this does not work I apologize as I don't have the time to test it out right now. I may get a spare moment later on in the evening, and if I do I will try to get something working.

    mark.
    • CommentAuthorselsine
    • CommentTimeMay 5th 2007
     
    Ahh I was able to figure out what you need to do in order to make it work. What you is an event box to get the events for you, since the gtk.image widget does not have its own window and doesn't seem to get many events.

    Here is a quick example of how to get everything working, you will need some images files names "char.png" and "diamond.png" in the same directory as your python script file.


    #!/usr/bin/env python

    import pygtk
    pygtk.require('2.0')
    import gtk
    import os

    class ImageClick(object):

    def __init__(self):
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.connect("destroy", self.destroy)
    self.window.set_border_width(10)
    #get the curront path
    self.folder = os.path.abspath(os.path.dirname(__file__))

    #create the table
    self.table = gtk.Table(1, 1)
    #create the event box for the images event
    self.event_box = gtk.EventBox()
    self.event_box.set_events(gtk.gdk.BUTTON_PRESS_MASK)
    self.event_box.connect("button_press_event", self.button_press_callback)
    #add the event box to the table
    self.table.attach(self.event_box, 0, 1, 0, 1)
    #Create the image and add it to the event box
    self.img = gtk.Image()
    self.event_box.add(self.img)
    self.image_file = "char.png"
    self.img.set_from_file(os.path.join(self.folder, self.image_file))

    self.window.add(self.table)
    #show everything
    self.table.show()
    self.event_box.show()
    self.img.show()

    # and the window
    self.window.show()

    def destroy(self, widget, data=None):
    gtk.main_quit()

    def button_press_callback(self, widget, event, data=None):
    if (self.image_file != "char.png"):
    self.image_file = "char.png"
    else:
    self.image_file = "diamond.png"
    #Set the new image
    self.img.set_from_file(os.path.join(self.folder, self.image_file))

    def main(self):
    gtk.main()

    if __name__ == "__main__":
    image_click = ImageClick()
    image_click.main()


    Hope that helps!

    mark.
    • CommentAuthorhpl
    • CommentTimeMay 5th 2007
     
    Wooowww!!!

    in your second post I have found an efficient solution! ^__^

    you are great!!!!!!!!!!!!!!!

    Thank you very much!

    when I finish my little application I'll send you a copy :D

    max
    • CommentAuthorselsine
    • CommentTimeMay 22nd 2007
     
    I'm glad that it worked out for you hpl.

    Be sure to send my your application when you are finished, I'm interested in seeing what it will be!
Add your comments
    Username Password
  • Format comments as