Search code examples
pythonpython-2.7interfacepygtk

How to attach an image to a table's cell in python?


I'm using pygtk... so I want to put images in the cells of a table created with gtk.Table(4,5) The numbers are the dimensions.

What I've done was creating buttons and putting them in the cells, then I load the images and attach them to the buttons, but it's not working. I have:

class D:

    def __init__(self):

        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_border_width(300)

        self.table = gtk.Table(4, 5, True)
        self.window.add(self.table)

        button = gtk.Button("unicorn")
        image = gtk.Image()
        image.set_from_file("unicorn.png")
        image.show()
        button.set_image(image)
        button.set_size_request(20,20)

        self.table.attach(button, 0, 1, 0, 1)
        button.show()
        self.table.show()
        self.window.show_all()

    def main():
        gtk.main()
        return 0

    if __name__ == "__main__":
        D()
        main()

Solution

  • Solved, just needed to attach the image directly to the table like:

        self.table.attach(image,0,1,0,1)
    

    and works perfectly :D