简体   繁体   English

在PyGObject中将Gtk.ComboBox与Gtk.TreeStore一起使用

[英]Use Gtk.ComboBox with a Gtk.TreeStore in PyGObject

I would like to have a Gtk.ComboBox with elements displayed like a tree. 我想有一个Gtk.ComboBox ,其元素像树一样显示。 It means some of the rows should have indention depending on there level in the tree. 这意味着某些行应根据树中的级别缩进。

When I interprete the documentation correct it should be possible with using a Gtk.TreeStore as data structure (model) behinde the control. 当我正确解释文档时 ,应该可以使用Gtk.TreeStore作为控件背后的数据结构(模型)。

Maybe I am missinterpreting the docu and it is not possible to use Gtk.TreeStore with it? 也许我误解了文档,并且无法将Gtk.TreeStore与它一起使用?

But it doesn't work in my example. 但这在我的示例中不起作用。 I have experience with Gtk.TreeStore and Gtk.TreeView . 我对Gtk.TreeStoreGtk.TreeView有经验。

The example code 示例代码

#!/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()

A visual example 视觉例子 在此处输入图片说明

The answer it that it is not possible to use a Gtk.ComboBox with a Gtk.TreeStore like data structure inside. 答案是,不可能将Gtk.ComboBoxGtk.TreeStore类的数据结构一起使用。

There is only the workaround with indentions. 只有缩进的解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM