【问题标题】:GtkDropDown search not working with plain stringsGtkDropDown 搜索不适用于纯字符串
【发布时间】:2022-08-17 15:58:57
【问题描述】:

我正在尝试创建一个带有搜索栏的 Gtk DropDown 来对时区列表进行排序。
Gtk4 文档状态

GtkDropDown 知道如何从 GtkStringList 中的项目中获取字符串

这是我最初尝试使用的模型。
它还在set_enable_search() 方法描述中说

请注意,必须设置 GtkDropDown:expression 才能进行搜索。

我在 GitLab 和 GitHub 上查看了其他项目以及他们是如何做到的,但我发现的所有方法似乎都对我不起作用,在 python 中抛出错误或直接忽略文本输入。

经过多次尝试,这是最后一个在model.append(Timezone(_(tz))) 上抛出ValueError: row sequence has the incorrect number of elements 的sn-p

# unrelated code above
model = Gtk.ListStore(GObject.GType(Timezone))
for tz in pytz.common_timezones:
    model.append(Timezone(_(tz)))
dropdown = Gtk.DropDown()
dropdown.set_model(model)
dropdown.set_enable_search(True)
dropdown.set_expression(Gtk.PropertyExpression(GObject.GType(Timezone), None, \"name\"))
self.action_row.add_suffix(dropdown) # parent defined above
# unrelated code below

班级Timezone

from gi.repository import GObject

class Timezone(GObject.GObject):
    def __init__(self, name):
        __gtype_name__ = \"Timezone\"
        self.name = name

    def __len__(self):
        return len(self.name)

    def __str__(self):
        return self.name

我被卡住了,我缺少什么让它工作?

    标签: python gtk flatpak


    【解决方案1】:

    这是我认为您正在努力实现的示例:

    #!/usr/bin/env python3
    
    import gi
    import pytz
    
    gi.require_version("Gtk", "4.0")
    from gi.repository import Gtk  # noqa: E402
    
    
    class ApplicationWindow(Gtk.ApplicationWindow):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
            drop_down = self.create_drop_down()
            self.set_child(drop_down)
    
        def create_drop_down(self):
            string_list_items = "\n".ljust(11).join(
                [f"<item>{time_zone}</item>" for time_zone in pytz.common_timezones]
            )
    
            drop_down_ui_string = f"""<interface>
      <object class="GtkDropDown" id="drop-down">
        <property name="model">
          <object class="GtkStringList" id="string-list">
            <items>
              {string_list_items}
            </items>
          </object>
        </property>
        <property name="enable-search">true</property>
        <property name="expression">
          <lookup type="GtkStringObject" name="string"></lookup>
        </property>
      </object>
    </interface>"""
    
            builder = Gtk.Builder.new_from_string(drop_down_ui_string, -1)
            drop_down = builder.get_object("drop-down")
    
            return drop_down
    
    
    class Application(Gtk.Application):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.connect("activate", self.on_activate)
    
        def on_activate(self, app):
            self.win = ApplicationWindow(application=app)
            self.win.show()
    
    
    app = Application()
    app.run()
    

    几个cmets:

    • PyGObject 不支持 GtkExpression。当前使搜索功能在GtkDropDown 中工作的唯一方法是使用GtkBuilder
    • 正如documentation 提到的,选项以GListModel 的形式提供给GtkDropDown。这意味着您不能使用GtkListStore 设置模型。事实上,documentation 解释了GtkListStore 对象是一个列表模型,用于与GtkTreeView 小部件一起使用,这与GtkDropDown 不同。
    • 如果你真的想使用GtkListStore,那么你可以通过以下方式进行:
    import gi
    import pytz
    
    gi.require_version("Gtk", "4.0")
    from gi.repository import GObject, Gtk  # noqa: E402
    
    
    class Timezone(GObject.GObject):
        __gtype_name__ = "Timezone"
    
        def __init__(self, name, *args, **kwargs):
            super().__init__(*args, **kwargs)
    
            self.name = name
    
        def __len__(self):
            return len(self.name)
    
        def __str__(self):
            return self.name
    
    
    model = Gtk.ListStore.new([GObject.GType(Timezone)])
    for time_zone in pytz.common_timezones:
        model.append([Timezone(time_zone)])
    

    【讨论】:

      猜你喜欢
      • 2016-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多