I’m trying to use multiple selection in a flowbox to allow toggle a selected item, shift to select a range etc. I also expected that clicking without Ctrl would replace the current selection.
However, shift and ctrl make no difference and items are just added to the selection.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
class FlowBoxMultiSelectionWindow(Gtk.Window):
def __init__(self):
super().__init__(title="FlowBox Multiple Selection (Python)")
self.set_default_size(400, 300)
self.connect("destroy", Gtk.main_quit)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=5)
self.add(vbox)
self.flowbox = Gtk.FlowBox()
# Set the selection mode to multiple
self.flowbox.set_selection_mode(Gtk.SelectionMode.MULTIPLE)
self.flowbox.set_homogeneous(True)
self.flowbox.set_row_spacing(5)
self.flowbox.set_column_spacing(5)
self.flowbox.set_max_children_per_line(5)
# Add some children to the FlowBox
for i in range(20):
label = Gtk.Label(label=f"Item {i + 1}")
self.flowbox.add(label)
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.add(self.flowbox)
vbox.pack_start(scrolled_window, True, True, 0)
button = Gtk.Button(label="Print Selected")
button.connect("clicked", self.print_selected_children)
vbox.pack_start(button, False, False, 0)
def print_selected_children(self, button):
selected_children = self.flowbox.get_selected_children()
print("Selected children:")
for child in selected_children:
# Get the widget inside the FlowBoxChild
child_widget = child.get_child()
if isinstance(child_widget, Gtk.Label):
print(f" - {child_widget.get_label()}")
if __name__ == "__main__":
win = FlowBoxMultiSelectionWindow()
win.show_all()
Gtk.main()
What am I missing? from the description GTK_SELECTION_MULTIPLE sounds to do what I want.
GTK_SELECTION_MULTIPLE
Any number of elements may be selected. The Ctrl key may be used to enlarge the selection, and Shift key to select between the focus and the child pointed to. Some widgets may also allow Click-drag to select a range of elements.
I’ve tried both GTK3 and GTK4, but get same behaviour.
I’m using Ubuntu 24.04 and have checked (in a separate test) that the SHIFT and CONTROL masks are set on a clicked event handler.