假设我有一个表示产品列表的TreeView(带有支持TreeStore)。
产品具有以下规格:
此外,每个产品都有一个由其构成的组件列表。 我想在树的子节点中显示有关组件的数据(因此它基本上是2级树视图),但是顶级节点将具有与子节点不同的模型。 / p>
如何使用Glade完成它(或者如果是必要的话,通过Python + PyGObjecy)?
答案 0 :(得分:2)
每个TreeView只能有一个模型,但是您可以创建一个模型,其中两个(或更多)模型粘合在一起,并添加一些布尔列来控制该行应显示的模型。然后,添加一组CellRenderers并将其可见性映射到这些控制列。
这样做的难点在于您希望列的顶部显示名称,在这种情况下,可能难以订购信息。如果您可以使用线框来表示您希望树看起来像我可以提供基本示例实现。
==编辑==
这是一个例子:
<强> main.py 强>:
from gi.repository import Gtk
from os.path import abspath, dirname, join
WHERE_AM_I = abspath(dirname(__file__))
# My Model map
mm = {
'name' : 0,
'catalog_num' : 1,
'comment' : 2,
'component' : 3,
'is_top' : 4,
'is_child' : 5,
}
class MyApp(object):
def __init__(self):
"""
Build GUI
"""
# Build GUI from Glade file
self.builder = Gtk.Builder()
self.glade_file = join(WHERE_AM_I, 'gui.glade')
self.builder.add_from_file(self.glade_file)
# Get objects
go = self.builder.get_object
self.window = go('window')
self.treestore = go('treestore')
# Fill model
self._load_model()
# Connect signals
self.builder.connect_signals(self)
self.window.connect('delete-event', lambda x,y: Gtk.main_quit())
# Everything is ready
self.window.show()
def _load_model(self):
my_data = [
['Cheese', 'F001', 'This is the best cheese ever!', '', True, False],
['Pepperoni', 'F002', 'Delicious pepperoni :}', '', True, False],
['Pepperonni Pizza', 'P001', 'Yes, I\'m hungry :(', '', True, False],
['', '', None, 'Cheese', False, True],
['', '', None, 'Pepperonni', False, True],
]
parent = None
for i in my_data:
if i[mm['is_child']]:
self.treestore.append(parent, i)
else:
parent = self.treestore.append(None, i)
if __name__ == '__main__':
gui = MyApp()
Gtk.main()
<强> gui.glade 强>:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.0 -->
<object class="GtkTreeStore" id="treestore">
<columns>
<!-- column-name name -->
<column type="gchararray"/>
<!-- column-name catalog_num -->
<column type="gchararray"/>
<!-- column-name comment -->
<column type="gchararray"/>
<!-- column-name component -->
<column type="gchararray"/>
<!-- column-name is_top -->
<column type="gboolean"/>
<!-- column-name is_child -->
<column type="gboolean"/>
</columns>
</object>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="border_width">10</property>
<property name="title" translatable="yes">TreeView test</property>
<property name="window_position">center-always</property>
<property name="default_width">400</property>
<property name="default_height">300</property>
<child>
<object class="GtkScrolledWindow" id="scrolledwindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkTreeView" id="treeview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">treestore</property>
<property name="headers_visible">False</property>
<property name="headers_clickable">False</property>
<property name="search_column">1</property>
<property name="tooltip_column">2</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn_name">
<property name="title" translatable="yes">Name</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext_name"/>
<attributes>
<attribute name="visible">4</attribute>
<attribute name="text">0</attribute>
</attributes>
</child>
<child>
<object class="GtkCellRendererText" id="cellrenderertext_component"/>
<attributes>
<attribute name="visible">5</attribute>
<attribute name="text">3</attribute>
</attributes>
</child>
</object>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn_catalog_num">
<property name="title" translatable="yes">Catalog #</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext_catalog"/>
<attributes>
<attribute name="visible">4</attribute>
<attribute name="text">1</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
找到