我想有一个Gtk.ComboBox
,其元素显示为树。这意味着某些行应该具有缩进,具体取决于树中的级别。
当我解释documentation正确时,应该可以使用Gtk.TreeStore
作为控制的数据结构(模型)。
也许我误解了文档,并且不可能使用Gtk.TreeStore
吗?
但它在我的例子中不起作用。我有Gtk.TreeStore
和Gtk.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()
答案是,不可能使用Gtk.ComboBox
和Gtk.TreeStore
之类的数据结构。
只有缩进的解决方法。