How to add a right click menu to Gtk.ListBox?

Title says it all: How to add a right click menu to Gtk.ListBox?
I tried searching for it but couldn’t find anything. I’m probably using the wrong keywords.

Thanks!

Just add a gesture to it: corebird/TweetListBox.vala at cb-1.7 · baedert/corebird · GitHub

Thanks for the tip, unfortunately I’m still unable to make it work. This code adds GestureMultiPress to a ListBox. It should print “pressed” when I click somewhere inside the list.
Do you have any idea of what I’m doing wrong?

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


class ListBoxRowWithData(Gtk.ListBoxRow):
    def __init__(self, data):
        super(Gtk.ListBoxRow, self).__init__()
        self.data = data
        self.add(Gtk.Label(label=data))


class ListBoxWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="ListBox Demo")
        self.set_border_width(10)

        box_outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
        self.add(box_outer)

        listbox = Gtk.ListBox()
        items = "This is a sorted ListBox Fail".split()

        for item in items:
            listbox.add(ListBoxRowWithData(item))

        box_outer.pack_start(listbox, True, True, 0)

        gesture = Gtk.GestureMultiPress.new(listbox)
        # I've also tried CAPTURE
        gesture.set_propagation_phase(Gtk.PropagationPhase.BUBBLE)
        gesture.connect("pressed", lambda g, n, x, y: print("pressed"))

        listbox.show_all()


win = ListBoxWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Thanks!

That’s a python issue; you need to keep gesture around after your __init__. Otherwise the gesture will be destroyed at the end of __init__ and removed from the widget.

I feel so dumb right now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.