7

For each row in my treeview, I want 4 image buttons next to each other. They will act like radio buttons, with only one being activateable at a time. Each button has an 'on' and 'off' image.

How do I do this? I figured out how to put images there, and how to put togglebuttons, but this seems to require some more effort as there is no pre-built cellrenderer that does what I want.

Basically what'd solve my problem is figuring out how to make an image in a gtk.treeview clickable. any ideas?

gpoo
  • 8,408
  • 3
  • 38
  • 53
Claudiu
  • 224,032
  • 165
  • 485
  • 680

3 Answers3

2

Here is a short version without kiwi requirement.

class CellRendererClickablePixbuf(gtk.CellRendererPixbuf):

    __gsignals__ = {'clicked': (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                                (gobject.TYPE_STRING,))
                   }

    def __init__(self):
        gtk.CellRendererPixbuf.__init__(self)
        self.set_property('mode', gtk.CELL_RENDERER_MODE_ACTIVATABLE)

    def do_activate(self, event, widget, path, background_area, cell_area,
                    flags):
        self.emit('clicked', path)
schlamar
  • 9,238
  • 3
  • 38
  • 76
  • I'm trying to use your code but I'm getting `gi.repository.Gtk' object has no attribute 'CELL_RENDERER_MODE_ACTIVATABLE'` - any ideas why? – pawel.ad Sep 17 '15 at 18:57
2

Have a look at this 'http://www.daa.com.au/pipermail/pygtk/2010-March/018355.html'. It shows you how to make a gtk.CellRendererPixbuf activatable, and able to connect to a click event signal.

cell = CellRendererPixbufXt()
cell.connect('clicked', func)

Update

As pointed out this answer, or the reference given doesn't work as advertised. It's missing the do_activate method, which needs to emit the clicked signal. Once it's done that, then the cell.connect will work.

Sorry if this answer mislead anyone.

James Hurford
  • 2,018
  • 19
  • 40
  • I don't know what you're looking at but my reference does. I notice your answer does pretty much the same thing as the reference I gave. If you're complaining about something else, please be a little clearer – James Hurford Jan 17 '13 at 06:10
  • The `clicked` signal is never triggered. Actually, this isn't a big surprise if I quote from your reference: "but i'm not sure how to emit the custom "clicked" signal, when the user clicks on the CellRenderer." – schlamar Jan 17 '13 at 06:56
  • At least your're being clearer now. Why you didn't say this in the first place. I'll check it out when I have the time, but it's been over a year, so I'm in no hurry. I'm surprised it got accepted in the first place if it doesn't work. – James Hurford Jan 17 '13 at 08:24
  • The link is down. You could you extend your answer, please? – ikreb Mar 14 '19 at 14:38
  • I think the answer below mine by schlamar is a better answer and gives an example. Really it should be marked as the correct answer, not mine. Honestly I've not touched GTK for a few years now. – James Hurford Mar 17 '19 at 08:58
1

Here is what worked for me:

class CellRendererClickablePixbuf(gtk.CellRendererPixbuf):
    gsignal('clicked', str)
    def __init__(self):
        gtk.CellRendererPixbuf.__init__(self)
        self.set_property('mode', gtk.CELL_RENDERER_MODE_ACTIVATABLE)
    def do_activate(self, event, widget, path, background_area, cell_area, flags):
        self.emit('clicked', path)
klimkin
  • 573
  • 5
  • 10