在PyGObject中使用Gtk.ComboBox和Gtk.TreeStore

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

我想有一个Gtk.ComboBox,其元素显示为树。这意味着某些行应该具有缩进,具体取决于树中的级别。

当我解释documentation正确时,应该可以使用Gtk.TreeStore作为控制的数据结构(模型)。

也许我误解了文档,并且不可能使用Gtk.TreeStore吗?

但它在我的例子中不起作用。我有Gtk.TreeStoreGtk.TreeView的经验。

示例代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)

        # The Model
        store = Gtk.TreeStore(int, str)
        # first item in the row is an internal ID that should not
        # be displayed in the combo box
        it = store.append(parent=None, row=[1, "Eins"])
        it = store.append(parent=it, row=[2, "Zwei"])
        it = store.append(parent=it, row=[3, "Drei"])

        # expected result
        # Eins
        # |- Zwei
        #  |- Drei

        # The View
        combo = Gtk.ComboBox.new_with_model(store)
        renderer = Gtk.CellRendererText()
        combo.pack_start(renderer, False)
        combo.add_attribute(renderer, "text", 1)

        box = Gtk.VBox()
        box.add(combo)
        self.add(box)

if __name__ == '__main__':
    window = MyWindow()
    window.show_all()
    Gtk.main()

一个视觉例子enter image description here

python-3.x combobox gtk pygtk pygobject
1个回答
0
投票

答案是,不可能使用Gtk.ComboBoxGtk.TreeStore之类的数据结构。

只有缩进的解决方法。

© www.soinside.com 2019 - 2024. All rights reserved.