Run the following program and then click on the line dividing the two headers as you would do to change the width of the columns. On clicking on that line, the first row of the treeview gets selected. Is that behavior normal? Is there a way to prevent it?
Note that the selection occurs only the first time that line gets clicked. If you subsequently unselect the first row (ctrl-leftclick), clicking the dividing line no longer selects the first row.
import random
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# Create a list of tuples of random strings.
def ranstr():
strlen = random.randrange(3, 10)
return ''.join(chr(random.choice(range(ord('a'), ord('a') + 26)))
for _ in range(strlen))
ranlist = [(ranstr().capitalize(), ranstr().capitalize()) for i in range(10)]
class TreeViewWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.connect('destroy', Gtk.main_quit)
self.show()
liststore = Gtk.ListStore(str, str)
for item in ranlist:
liststore.append(item)
treeview = Gtk.TreeView()
treeview.set_model(liststore)
treeview.show()
renderer = Gtk.CellRendererText()
for i in range(2):
column = Gtk.TreeViewColumn()
column.set_title(f'Column{i}')
column.pack_start(renderer, True)
column.add_attribute(renderer, 'text', i)
column.set_resizable(True)
column.set_fixed_width(150)
treeview.append_column(column)
self.add(treeview)
win = TreeViewWindow()
Gtk.main()