Glade, Python, GTK3:数据的列表视图
经过几个小时的折腾,我遇到了一些我以为很简单的问题(在GTK-2中确实很简单),所以我想问一下。抱歉这次没有代码或具体细节,因为我现在完全没有任何东西能正常工作。
我正在写一个应用程序,它需要从数据库中提取一些数据,并以表格的形式展示出来。这种事情我觉得很常见,但我就是搞不定。我找不到合适的教程(那些教程对我来说也不管用,因为我的窗口里不止有一个ListStore)。我在用Glade设计我的用户界面,它有一个笔记本控件,里面有一个网格,放着各种东西,包括一个应该放列表的地方。
我尝试添加一个ListStore对象,但就是无法显示出来。我用的是Python 2.7.6和Glade 3.16.1。
self.liststore = self.builder.get_object('liststore1')
self.liststore.append(['1st column','2nd column'])
这个本来是用来显示数据的,但它没有显示出来。我在Glade中无法预览ListStore,只能把它作为顶层对象添加,而不能放到它应该放的地方。
1 个回答
0
这是一个非常简单的例子,只展示了一列和两个条目。其中一个是在glade文件中创建的,另一个是用python创建的,这样你就可以看到如何修改列表存储:
glade文件内容:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkListStore" id="liststore1">
<columns>
<!-- column-name test -->
<column type="gchararray"/>
</columns>
<data>
<row>
<col id="0" translatable="yes">entry</col>
</row>
</data>
</object>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="default_width">247</property>
<property name="default_height">188</property>
<child>
<object class="GtkTreeView" id="treeview1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">liststore1</property>
<child internal-child="selection">
<object class="GtkTreeSelection" id="treeview-selection1"/>
</child>
<child>
<object class="GtkTreeViewColumn" id="treeviewcolumn1">
<property name="title" translatable="yes">test-column</property>
<child>
<object class="GtkCellRendererText" id="cellrenderertext1"/>
<attributes>
<attribute name="text">0</attribute>
</attributes>
</child>
</object>
</child>
</object>
</child>
</object>
</interface>
这是python文件内容:
from gi.repository import Gtk
class Test:
def __init__(self):
builder = Gtk.Builder()
builder.add_from_file('test.glade')
self.liststore = builder.get_object('liststore1')
#append something with python:
self.liststore.append(('stackoverflow',))
window = builder.get_object('window1')
window.connect('delete-event', Gtk.main_quit)
window.show_all()
if __name__ == '__main__':
Test()
Gtk.main()