开发者

pySQLite integration to pyGtk treeview

Is there some simple way or how to integra开发者_StackOverflowte sqlite database into gtk.TreeModel (TreeView)?

What I want is to display data from a db in treeview, and it would be nice when I change something in db to see the change in treeview and the other way round.


Let's say you put all your data in itemlist:

cur.execute('''select * from warehouse''')
itemlist = cur.fetchall()

To get list of column properties, do:

cur.execute('''PRAGMA table_info(warehouse)''')

To get only names of columns, you can then do:

colnames = [ i[1] for i in cur.fetchall() ]

Later you may need to set types in a ListStore for your TreeView, so do something like:

coltypes = [ i[2] for i in cur.fetchall() ]
for index, item in enumerate(coltypes):
    if (item == 'char'):
        coltypes[index] = str
    elif (item == 'integer'):
        coltypes[index] = int

Then put all info from your database to a TreeView:

store = gtk.ListStore(*coltypes)
for act in itemlist:
    store.append(act)

Now create columns for the TreeView (let's say it's called 'tree'):

for index, item in enumerate(colnames):
rendererText = gtk.CellRendererText()
column = gtk.TreeViewColumn(item, rendererText, text=index)
column.set_sort_column_id(index)
tree.append_column(column)

After that you should have automatically generated TreeView widget with correct columns and all the data. Source: Advanced widgets in PyGTK

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜