Monday, 7 December 2020

Arrow keys ignore selection via Gtk.FlowBox.select_child()

When I use Gtk.FlowBox.select_child() programmatically the selection works fine. But if I use arrow keys to move the selection afterwards, it ignores the previously selected item and moves from the first item in the FlowBox. I don't know if this is a bug or I have to do something else to make the cursor know the new location. Selecting with mouse works as expected.

I'm using gtk3 version 3.24.23 on Linux.

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

# just a folder with a bunch of png images. 
ICON_DIR = os.path.expanduser("~/.icons")

class Window(Gtk.Window):
    
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(480, 400)
        self.scr_win = Gtk.ScrolledWindow(parent=self)
        self.flowbox = Gtk.FlowBox(parent=self.scr_win, homogeneous=True, 
                row_spacing=8, column_spacing=8, min_children_per_line=4, 
                max_children_per_line=100)
        
        for i, icon_name in enumerate(os.listdir(ICON_DIR)):
            self.flowbox.insert(
                    Gtk.Image.new_from_pixbuf( 
                    GdkPixbuf.Pixbuf.new_from_file_at_size(
                    os.path.join(ICON_DIR, icon_name), 64, 64)), i)
        
        self.show_all()

        # selecting the 51st item just as an example:
        selected = self.flowbox.get_child_at_index(50)
        self.flowbox.select_child(selected)
        self.scr_win.get_vadjustment().set_value(selected.get_allocation().y)
            
if __name__ == "__main__":
    win = Window()
    win.connect("destroy", Gtk.main_quit)
    Gtk.main()
    
    


from Arrow keys ignore selection via Gtk.FlowBox.select_child()

No comments:

Post a Comment