对 Gtk.ListBox 中的行进行排序

问题描述 投票:0回答:1

我正在尝试根据搜索查询对

Gtk.ListBox
中的行进行排序。 GTK 似乎有一种使用
set_sort_func()
执行此操作的本机方法,但是我找不到任何关于如何使用它的好示例。有人可以提供一个如何做到这一点的例子吗?

python sorting gtk
1个回答
0
投票

你没有说你正在使用什么版本的 Gtk,所以我假设它是 Gtk4。以下是如何在 Gtk.ListBox 中使用排序功能的最小示例代码。

警告:如果Gtk.ListBox是基于模型构建的,则无法使用此功能。


    import random
    from gi.repository import Gtk
    import gi
    gi.require_version('Gtk', '4.0')
    
    
    class MainWindow(Gtk.ApplicationWindow):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
            self.set_child(self.box)
    
            self.button = Gtk.Button(label="Insert Row")
            self.box.append(self.button)
    
            self.set_default_size(800, 600)
    
            self.list_box = Gtk.ListBox.new()
            self.list_box.set_sort_func(self.sort_function)
    
            self.box.append(self.list_box)
    
            self.button.connect('clicked', self.add)
    
        def add(self, button):
    
            number = random.randint(0, 100)
            row = self.row_setup(f"test {number}", f"{number} ")
    
            self.list_box.append(row)
    
        def sort_function(self, one, two):
    
            one_number = int(one.get_child().get_first_child().props.label)
            two_number = int(two.get_child().get_first_child().props.label)
    
            if one_number < two_number:
                return -1
            elif one_number > two_number:
                return 1
            else:
                return 0
    
        def row_setup(self, title, number):
    
            row = Gtk.ListBoxRow.new()
            row_box = Gtk.Box()
            row_title = Gtk.Label.new(title)
            row_number = Gtk.Label.new(number)
            row_box.append(row_number)
            row_box.append(row_title)
            row.set_child(row_box)
    
            return row
    
    
    class MyApp(Gtk.Application):
        def __init__(self, **kwargs):
            super().__init__(**kwargs)
            self.connect('activate', self.on_activate)
    
        def on_activate(self, app):
            self.win = MainWindow(application=app)
            self.win.present()
    
    
    app = MyApp(application_id="com.example.GtkApplication")
    app.run(None)
© www.soinside.com 2019 - 2024. All rights reserved.