Python-Ref > GUI programming with PyGTK > Data structures > ListStore
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce
GTK Widgety

ListStore

Data model for table like, 2D data.
ListStore is an implementation of the TreeModel interface.
The example below shows how a ListStore is created and manipulated.
Expand/Shrink
  1   import gtk
  2   
  3   # table of 3 integer columns
  4   store = gtk.ListStore( int, int, int)
  5   for i in range( 10):
  6     store.append( [i, i**2, i**3])
  7   # append, prepend, insert, etc. return an TreeIter instance
  8   iter1 = store.prepend( [-1, 1, -1])
  9   iter2 = store.insert( 5, [-2, 4, -8])
 10   iter3 = store.insert_before( iter1, [-3, 9, -27])
 11   iter4 = store.insert_after( iter1, [-4, 16, -64])
 12   # move the item2 after item2 
 13   store.move_after( iter2, iter1)
 14   # swap items
 15   store.swap( iter3, iter4)
 16   # remove an item
 17   store.remove( iter3)
 18   # set one value in a row (iter, column, value)
 19   store.set_value( iter1, 1, 10)
 20   
 21   # iterate over the store
 22   print store[0]
 23   for row in store:
 24     for value in row:
 25       print value,
 26     print
stdout:
<gtk.TreeModelRow object at 0x2b0083207ce0>
-4 16 -64
-1 10 -1
-2 4 -8
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
Doba běhu: 69.5 ms
The following example shows the effect of strong typing of ListStore data - you cannot use a different type than you declared in your data model.
Expand/Shrink
  1   import gtk
  2   
  3   # table of 2 strings and 1 integer columns
  4   store = gtk.ListStore( str, str, int)
  5   store.append( ["A","B",1])
  6   store.append( ["A","B","1"])
stderr:
Traceback (most recent call last):
  File "gtk-liststore1-2.py", line 6, in ?
    store.append( ["A","B","1"])
TypeError: value is of wrong type for this column
Doba běhu: 66.6 ms