GTK - Python 树视图分隔符函数

GTK - Python Treeview Separator Function

我需要在 GTK3 树视图的行之间显示分隔符,但它给出了错误。我已阅读 documentation 但我无法修复它 我无法在网上找到此功能的示例。

为了能够看到代码的工作版本(没有分隔符),切换 treeview1.set_model(treestore1) 行的注释并注释掉以下三行代码。

这里是简化代码(Python 3.7):

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

builder = Gtk.Builder()
builder.add_from_file('test1.glade')
window1 = builder.get_object('window1')
treeview1 = builder.get_object('treeview1')

class Signals:
    def on_window1_destroy(self, widget):
        Gtk.main_quit()

# First column of the data is for separator function
list_data = [[False, "text1", "data1"],
             [True, "text2", "data1"],
             [False, "text4", "data5"]]

treestore1 = Gtk.TreeStore(bool, str, str)
piter1 = treestore1.append(None, list_data[0])
piter2 = treestore1.append(None, list_data[1])
piter3 = treestore1.append(None, list_data[2])

#treeview1.set_model(treestore1)

# Following three lines are added for separator function
iter = treestore1.get_iter(2)
sep_func = Gtk.TreeViewRowSeparatorFunc(treestore1, iter, data=0)
treeview1.set_model(Gtk.TreeModelSort(model=sep_func))

for i, column_title in enumerate(["col1", "col2"]):
    renderer = Gtk.CellRendererText()
    column = Gtk.TreeViewColumn(column_title, renderer, text=i+1)
    treeview1.append_column(column)
treeview1.expand_all()

builder.connect_signals(Signals())
window1.show_all()
Gtk.main()

这是 Glade (test1.glade) 文件。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <property name="default_width">300</property>
    <property name="default_height">300</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkTreeView" id="treeview1">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <child internal-child="selection">
          <object class="GtkTreeSelection"/>
        </child>
      </object>
    </child>
  </object>
</interface>

这里是缩短的错误:

Traceback (most recent call last):
.
.
.
  File "/home/user/.local/lib/python3.7/site-packages/gi/module.py", line 218, in __getattr__
    raise NotImplementedError(info)
NotImplementedError: gi.CallbackInfo(TreeViewRowSeparatorFunc)

如何以正确的方式编写树视图行的单独函数?

TreeViewRowSeparatorFunc只是C语言中使用的类型定义。在 Python 中,我们不使用它,而是使用一个普通函数 returns 该行是否应该是分隔符。这个问题让我有点摸不着头脑。

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

builder = Gtk.Builder()
builder.add_from_file('test1.glade')
window1 = builder.get_object('window1')
treeview1 = builder.get_object('treeview1')

class Signals:
    def on_window1_destroy(self, widget):
        Gtk.main_quit()

# First column of the data is for separator function
list_data = [[False, "text1", "data1"],
             [True, "text2", "data1"],
             [False, "text4", "data5"]]

treestore1 = Gtk.TreeStore(bool, str, str)
piter1 = treestore1.append(None, list_data[0])
piter2 = treestore1.append(None, list_data[1])
piter3 = treestore1.append(None, list_data[2])

# Following lines are added for separator function
def separator_function (model, iter_):
   return model[iter_][0]

treeview1.set_row_separator_func (separator_function)


treeview1.set_model(treestore1)

for i, column_title in enumerate(["col1", "col2"]):
    renderer = Gtk.CellRendererText()
    column = Gtk.TreeViewColumn(column_title, renderer, text=i+1)
    treeview1.append_column(column)
treeview1.expand_all()

builder.connect_signals(Signals())
window1.show_all()
Gtk.main()