Home > database >  Getting the Gtk.TreePath of the first row in a Gtk.TreeView
Getting the Gtk.TreePath of the first row in a Gtk.TreeView

Time:09-15

I want to have the first row of a Gtk.TreeView selected upon a button press. I'm aware that this is possible using row_activated, however row_activated takes a Gtk.TreePath and a Gtk.TreeViewColumn (which I assume can be set to None). The issue is that I do not know how to get the Gtk.TreePath of the first row.

class Main(Gtk.Window):
    def __init__(self):
        super().__init__()

        liststore = Gtk.ListStore(str)
        liststore.append(['first_entry'])
        liststore.append(['second_entry'])
        liststore.append(['third_entry'])

        treeview = Gtk.TreeView()
        treeview.set_model(liststore)

        renderer = Gtk.CellRendererText()
        column = Gtk.TreeViewColumn('Item', renderer, text=0)
        treeview.append_column(column)

        button = Gtk.Button(label='button')

        box = Gtk.Box()
        box.set_orientation(Gtk.Orientation.VERTICAL)
        box.add(button)
        box.add(treeview)
        
        self.add(box)


if __name__ == '__main__':
    main = Main()
    main.show_all()
    Gtk.main()

In this case, when the button is pressed, I want first_entry (which is the first row) to be selected

CodePudding user response:

I found treeview.set_cursor(0) in question: Python GTK: How to set a selected row on gtk.treeview - Stack Overflow and I think it can be better solution.

BTW: you have to use self. to access treeview in function executed by button.

import gi

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

class Main(Gtk.Window):
    def __init__(self):
        super().__init__()

        liststore = Gtk.ListStore(str)
        liststore.append(['first_entry'])
        liststore.append(['second_entry'])
        liststore.append(['third_entry'])

        self.treeview = Gtk.TreeView()
        self.treeview.set_model(liststore)

        renderer = Gtk.CellRendererText()
       
        column = Gtk.TreeViewColumn('Item', renderer, text=0)
        self.treeview.append_column(column)

        button = Gtk.Button(label='button')
        button.connect('clicked', self.select_first)
        
        box = Gtk.Box()
        box.set_orientation(Gtk.Orientation.VERTICAL)
        box.add(button)
        box.add(self.treeview)
        
        self.add(box)

    def select_first(self, event):
        self.treeview.set_cursor(0)
        
        
if __name__ == '__main__':
    main = Main()
    main.show_all()
    Gtk.main()

Doc for set_cursor() for C/C (but can be useful).

It seems original set_cursor() also needs TreePath but in Python it works with index. Other functions don't have to work with index instead of TreePath


EDIT:

I found you can create TreePath using index - as list or string

def select_first(self, event):
    path = Gtk.TreePath.new_from_indices([0]) # it needs list because trees can be nestes, ie. [0,5,3]
    #path = Gtk.TreePath.new_from_string("0")  # for nested trees it can be ie. "0:5:3"

    selection = self.treeview.get_selection()

    selection.select_path(path)  

EDIT:

If you use self. when you create self.column = Gtk.TreeViewColumn('Item', renderer, text=0) then you can use it with row_activated() but this doesn't select row on my computer (but doesn't raise error)

def select_first(self, event):
    path = Gtk.TreePath.new_from_indices([0])
    self.treeview.row_activated(path, self.column)

Full code which I used for tests:

# https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeSelection.html#gtk-treeselection
# https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreePath.html#Gtk.TreePath.new_from_indices

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

class Main(Gtk.Window):
    def __init__(self):
        super().__init__()

        self.connect("destroy", Gtk.main_quit)
        
        liststore = Gtk.ListStore(str)
        liststore.append(['first_entry'])
        liststore.append(['second_entry'])
        liststore.append(['third_entry'])

        self.treeview = Gtk.TreeView()
        self.treeview.set_model(liststore)

        renderer = Gtk.CellRendererText()
       
        self.column = Gtk.TreeViewColumn('Item', renderer, text=0)
        self.treeview.append_column(self.column)

        button1 = Gtk.Button(label='First')
        button1.connect('clicked', self.select_first)

        button2 = Gtk.Button(label='Second')
        button2.connect('clicked', self.select_second)

        button3 = Gtk.Button(label='Third')
        button3.connect('clicked', self.select_third)

        box = Gtk.Box()
        box.set_orientation(Gtk.Orientation.VERTICAL)
        box.add(button1)
        box.add(button2)
        box.add(button3)
        box.add(self.treeview)
        
        self.add(box)

    def select_first(self, event):
        self.treeview.set_cursor(0)
        
    def select_second(self, event):
        path = Gtk.TreePath.new_from_indices([1])  # it needs list because trees can be nestes, ie. [0,5,3]
        path = Gtk.TreePath.new_from_string("1")   # for nested trees it can be ie. "0:5:3"
        selection = self.treeview.get_selection()
        selection.select_path(path)  
        
    def select_third(self, event):
        path = Gtk.TreePath.new_from_indices([2])  # it needs list because trees can be nestes, ie. [0,5,3]
        self.treeview.row_activated(path, self.column)
        
if __name__ == '__main__':
    main = Main()
    main.show_all()
    Gtk.main()
  • Related