在Gtk.ListBox中对行进行排序

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

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是基于模型构建的,则无法使用此函数。

  1. import random
  2. from gi.repository import Gtk
  3. import gi
  4. gi.require_version('Gtk', '4.0')
  5. class MainWindow(Gtk.ApplicationWindow):
  6. def __init__(self, *args, **kwargs):
  7. super().__init__(*args, **kwargs)
  8. self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
  9. self.set_child(self.box)
  10. self.button = Gtk.Button(label="插入行")
  11. self.box.append(self.button)
  12. self.set_default_size(800, 600)
  13. self.list_box = Gtk.ListBox.new()
  14. self.list_box.set_sort_func(self.sort_function)
  15. self.box.append(self.list_box)
  16. self.button.connect('clicked', self.add)
  17. def add(self, button):
  18. number = random.randint(0, 100)
  19. row = self.row_setup(f"测试 {number}", f"{number} ")
  20. self.list_box.append(row)
  21. def sort_function(self, one, two):
  22. one_number = int(one.get_child().get_first_child().props.label)
  23. two_number = int(two.get_child().get_first_child().props.label)
  24. if one_number < two_number:
  25. return -1
  26. elif one_number > two_number:
  27. return 1
  28. else:
  29. return 0
  30. def row_setup(self, title, number):
  31. row = Gtk.ListBoxRow.new()
  32. row_box = Gtk.Box()
  33. row_title = Gtk.Label.new(title)
  34. row_number = Gtk.Label.new(number)
  35. row_box.append(row_number)
  36. row_box.append(row_title)
  37. row.set_child(row_box)
  38. return row
  39. class MyApp(Gtk.Application):
  40. def __init__(self, **kwargs):
  41. super().__init__(**kwargs)
  42. self.connect('activate', self.on_activate)
  43. def on_activate(self, app):
  44. self.win = MainWindow(application=app)
  45. self.win.present()
  46. app = MyApp(application_id="com.example.GtkApplication")
  47. 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.

  1. import random
  2. from gi.repository import Gtk
  3. import gi
  4. gi.require_version(&#39;Gtk&#39;, &#39;4.0&#39;)
  5. class MainWindow(Gtk.ApplicationWindow):
  6. def __init__(self, *args, **kwargs):
  7. super().__init__(*args, **kwargs)
  8. self.box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
  9. self.set_child(self.box)
  10. self.button = Gtk.Button(label=&quot;Insert Row&quot;)
  11. self.box.append(self.button)
  12. self.set_default_size(800, 600)
  13. self.list_box = Gtk.ListBox.new()
  14. self.list_box.set_sort_func(self.sort_function)
  15. self.box.append(self.list_box)
  16. self.button.connect(&#39;clicked&#39;, self.add)
  17. def add(self, button):
  18. number = random.randint(0, 100)
  19. row = self.row_setup(f&quot;test {number}&quot;, f&quot;{number} &quot;)
  20. self.list_box.append(row)
  21. def sort_function(self, one, two):
  22. one_number = int(one.get_child().get_first_child().props.label)
  23. two_number = int(two.get_child().get_first_child().props.label)
  24. if one_number &lt; two_number:
  25. return -1
  26. elif one_number &gt; two_number:
  27. return 1
  28. else:
  29. return 0
  30. def row_setup(self, title, number):
  31. row = Gtk.ListBoxRow.new()
  32. row_box = Gtk.Box()
  33. row_title = Gtk.Label.new(title)
  34. row_number = Gtk.Label.new(number)
  35. row_box.append(row_number)
  36. row_box.append(row_title)
  37. row.set_child(row_box)
  38. return row
  39. class MyApp(Gtk.Application):
  40. def __init__(self, **kwargs):
  41. super().__init__(**kwargs)
  42. self.connect(&#39;activate&#39;, self.on_activate)
  43. def on_activate(self, app):
  44. self.win = MainWindow(application=app)
  45. self.win.present()
  46. app = MyApp(application_id=&quot;com.example.GtkApplication&quot;)
  47. 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:

确定