检查下面的示例是否适合您。它从 glade 文件加载 ui 并使用 sqlite 数据库来存储项目。
脚本:
import gtk
import sqlite3
class ListViewTestApp:
def __init__(self):
builder = gtk.Builder()
builder.add_from_file('listview_test.glade')
builder.connect_signals(self)
self.model = builder.get_object('list_items')
self.list = builder.get_object('items_view')
column = gtk.TreeViewColumn('column0', gtk.CellRendererText(), text=0)
column.set_clickable(True)
column.set_resizable(True)
self.list.append_column(column)
column = gtk.TreeViewColumn('column1', gtk.CellRendererText(), text=0)
column.set_clickable(True)
column.set_resizable(True)
self.list.append_column(column)
self.create_database()
self.load_list_items()
window = builder.get_object('main_window')
window.show_all()
def create_database(self):
self.engine = sqlite3.connect(':memory:')
self.engine.execute('create table test_table ' + \
'(id INTEGER NOT NULL PRIMARY KEY, test_field0 VARCHAR(30), test_field1 VARCHAR(30))');
self.add_new_line('test0', 'test000');
self.add_new_line('test1', 'test111');
def load_list_items(self):
self.model.clear()
result = self.engine.execute('select * from test_table');
for row in result:
self.model.append([row[1], row[2]])
def add_new_line(self, test0, test1):
query = 'INSERT INTO test_table (test_field0, test_field1) VALUES ("{0}", "{1}")'\
.format(test0, test1)
self.engine.execute(query)
def on_load_button_clicked(self, widget):
self.load_list_items()
def on_add_line_button_clicked(self, widget):
id = len(self.model)
self.add_new_line('new_item{0}'.format(id), 'new__item{0}'.format(id));
self.load_list_items()
if __name__ == "__main__":
ListViewTestApp()
gtk.main()
林间空地文件(listview_test.glade):
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkListStore" id="list_items">
<columns>
<!-- column-name column0 -->
<column type="gchararray"/>
<!-- column-name column1 -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="main_window">
<child>
<object class="GtkFixed" id="fixed1">
<property name="visible">True</property>
<child>
<object class="GtkTreeView" id="items_view">
<property name="width_request">270</property>
<property name="height_request">250</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="model">list_items</property>
</object>
<packing>
<property name="x">200</property>
<property name="y">20</property>
</packing>
</child>
<child>
<object class="GtkButton" id="load_button">
<property name="label" translatable="yes">load</property>
<property name="width_request">100</property>
<property name="height_request">40</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_load_button_clicked"/>
</object>
<packing>
<property name="x">54</property>
<property name="y">49</property>
</packing>
</child>
<child>
<object class="GtkButton" id="add_line_button">
<property name="label" translatable="yes">add line</property>
<property name="width_request">100</property>
<property name="height_request">40</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="xalign">0.41999998688697815</property>
<property name="yalign">0.46000000834465027</property>
<signal name="clicked" handler="on_add_line_button_clicked"/>
</object>
<packing>
<property name="x">54</property>
<property name="y">113</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
希望这会有所帮助,问候