英文:
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('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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论