簡體   English   中英

在PyGObject中將Gtk.ComboBox與Gtk.TreeStore一起使用

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

我想有一個Gtk.ComboBox ,其元素像樹一樣顯示。 這意味着某些行應根據樹中的級別縮進。

當我正確解釋文檔時 ,應該可以使用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()

視覺例子 在此處輸入圖片說明

答案是,不可能將Gtk.ComboBoxGtk.TreeStore類的數據結構一起使用。

只有縮進的解決方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM