在Gtk.ListBox中对行进行排序

huangapple go评论100阅读模式
英文:

Sorting rows in a Gtk.ListBox

问题

我正在尝试根据搜索查询对Gtk.ListBox中的行进行排序。似乎GTK有一种使用set_sort_func()来实现这一功能的本地方法,但是我找不到任何好的示例来说明如何使用它。是否有人可以提供一个示例来演示如何实现这个功能?

英文:

I'm trying to sort rows in a Gtk.ListBox based on a search query. It seems that GTK has a native way of doing this with set_sort_func(), however I can't find any good examples on how to use it. Could someone provide an example of how this may be done?

答案1

得分: 1

你没有说你使用的是哪个版本的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="插入行")
        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"测试 {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)
英文:

You didn't say what version of Gtk you're using, so I'm going to assume it's Gtk4. Below is a minimal example code for how to use the sort function in a Gtk.ListBox.

Warning: This function cannot be used if the Gtk.ListBox is built based on a model.


    import random
    from gi.repository import Gtk
    import gi
    gi.require_version(&#39;Gtk&#39;, &#39;4.0&#39;)
    
    
    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=&quot;Insert Row&quot;)
            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(&#39;clicked&#39;, self.add)
    
        def add(self, button):
    
            number = random.randint(0, 100)
            row = self.row_setup(f&quot;test {number}&quot;, f&quot;{number} &quot;)
    
            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 &lt; two_number:
                return -1
            elif one_number &gt; 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(&#39;activate&#39;, self.on_activate)
    
        def on_activate(self, app):
            self.win = MainWindow(application=app)
            self.win.present()
    
    
    app = MyApp(application_id=&quot;com.example.GtkApplication&quot;)
    app.run(None)

huangapple
  • 本文由 发表于 2023年8月9日 01:18:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定