Gtk.TreeStore
Gtk.TreeStore — A tree-like data structure that can be used with the Gtk.TreeView
Object Hierarchy:
GObject
╰── Gtk.TreeStore
See also:
Functions:
- new
(types:list) -> Gtk.TreeStore
- set_column_types
(self, types:list)
- set_value
(self, iter:Gtk.TreeIter, column:int, value:GObject.Value)
- set
(self, iter:Gtk.TreeIter, columns:list, values:list)
- remove
(self, iter:Gtk.TreeIter) -> bool
- insert
(self, parent:Gtk.TreeIter=None, position:int) -> iter:Gtk.TreeIter
- insert_before
(self, parent:Gtk.TreeIter=None, sibling:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
- insert_after
(self, parent:Gtk.TreeIter=None, sibling:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
- insert_with_values
(self, parent:Gtk.TreeIter=None, position:int, columns:list, values:list) -> iter:Gtk.TreeIter
- prepend
(self, parent:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
- append
(self, parent:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
- is_ancestor
(self, iter:Gtk.TreeIter, descendant:Gtk.TreeIter) -> bool
- iter_depth
(self, iter:Gtk.TreeIter) -> int
- clear
(self)
- iter_is_valid
(self, iter:Gtk.TreeIter) -> bool
- swap
(self, a:Gtk.TreeIter, b:Gtk.TreeIter)
- move_before
(self, iter:Gtk.TreeIter, position:Gtk.TreeIter=None)
- move_after
(self, iter:Gtk.TreeIter, position:Gtk.TreeIter=None)
Description:
The Gtk.TreeStore
object is a list model for use with a GtkTreeView widget. It implements the Gtk.TreeModel interface, and consequentially, can use all of the methods available there. It also implements theGtk.TreeSortable interface so it can be sorted by the view. Finally, it also implements the tree drag and drop
interfaces.
Gtk.TreeStore as Gtk.Buildable:
The Gtk.TreeStore
implementation of the GtkBuildable interface allows to specify the model columns with a \type
attribute specifies the data type for the column.
An example of a UI Definition fragment for a tree store:
<object class="GtkTreeStore">
<columns>
<column type="gchararray"/>
<column type="gchararray"/>
<column type="gint"/>
</columns>
</object>
Function Details:
new()
new (types:list) -> Gtk.TreeStore
Creates a new tree store as with n_columns
columns each of the types passedin. Note that only types derived from standard GObject fundamental typesare supported.
As an example, Gtk.TreeStore:new (3, G_TYPE_INT, G_TYPE_STRING,GDK_TYPE_PIXBUF);
will create a new Gtk.TreeStore
with three columns, of typegint, gchararray, and GdkPixbuf respectively.
- Returns: a new Gtk.TreeStore
set_column_types()
set_column_types (self, types:list)
This function is meant primarily for GObjects that inherit from Gtk.TreeStore
, and should only be used when constructing a new Gtk.TreeStore
. It will not function after a row has been added, or a method on the Gtk.TreeModel
interface is called.
set_value()
set_value (self, iter:Gtk.TreeIter, column:int, value:GObject.Value)
Sets the data in the cell specified by iter
and column
.The type of value
must be convertible to the type of thecolumn.
set()
set (self, iter:Gtk.TreeIter, columns:list, values:list)
Sets the value of one or more cells in the row referenced by iter
.The variable argument list should contain integer column numbers,each column number followed by the value to be set.The list is terminated by a -1. For example, to set column 0 with typeG_TYPE_STRING
to “Foo”, you would writeGtk.TreeStore:set (store, iter, 0, "Foo", -1)
.
The value will be referenced by the store if it is a G_TYPE_OBJECT
, and itwill be copied if it is a G_TYPE_STRING
or G_TYPE_BOXED
.
remove()
remove (self, iter:Gtk.TreeIter) -> bool
Removes iter
from tree_store
. After being removed, iter
is set to thenext valid row at that level, or invalidated if it previously pointed to thelast one.
- Returns:
True
ifiter
is still valid,FALSE
if not.
insert()
insert (self, parent:Gtk.TreeIter=None, position:int) -> iter:Gtk.TreeIter
Creates a new row at position
. If parent is non-None
, then the row will bemade a child of parent
. Otherwise, the row will be created at the toplevel.If position
is -1 or is larger than the number of rows at that level, thenthe new row will be inserted to the end of the list. iter
will be changedto point to this new row. The row will be empty after this function iscalled. To fill in values, you need to call Gtk.TreeStore:set()
orGtk.TreeStore:set_value()
.
insert_before()
insert_before (self, parent:Gtk.TreeIter=None, sibling:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
Inserts a new row before sibling
. If sibling
is None
, then the row willbe appended to parent
’s children. If parent
and sibling
are None
, thenthe row will be appended to the toplevel. If both sibling
and parent
areset, then parent
must be the parent of sibling
. When sibling
is set,parent
is optional.
iter
will be changed to point to this new row. The row will be empty afterthis function is called. To fill in values, you need to callGtk.TreeStore:set()
or Gtk.TreeStore:set_value()
.
insert_after()
insert_after (self, parent:Gtk.TreeIter=None, sibling:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
Inserts a new row after sibling
. If sibling
is None
, then the row will beprepended to parent
’s children. If parent
and sibling
are None
, thenthe row will be prepended to the toplevel. If both sibling
and parent
areset, then parent
must be the parent of sibling
. When sibling
is set,parent
is optional.
iter
will be changed to point to this new row. The row will be empty afterthis function is called. To fill in values, you need to callGtk.TreeStore:set()
or Gtk.TreeStore:set_value()
.
insert_with_values()
insert_with_values (self, parent:Gtk.TreeIter=None, position:int, columns:list, values:list) -> iter:Gtk.TreeIter
Creates a new row at position
. iter
will be changed to point to thisnew row. If position
is -1, or larger than the number of rows on the list, thenthe new row will be appended to the list. The row will be filled withthe values given to this function.
CallingGtk.TreeStore:insert_with_values (tree_store, iter, position, ...)
has the same effect as calling
Gtk.TreeStore:insert (tree_store, iter, position);Gtk.TreeStore:set (tree_store, iter, ...); with the different that the former will only emit a row_inserted signal,while the latter will emit row_inserted, row_changed and if the tree storeis sorted, rows_reordered. Since emitting the rows_reordered signalrepeatedly can affect the performance of the program,Gtk.TreeStore:insert_with_values()
should generally be preferred wheninserting rows in a sorted tree store.
- Since: 2.10
prepend()
prepend (self, parent:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
Prepends a new row to tree_store
. If parent
is non-None
, then it will prependthe new row before the first child of parent
, otherwise it will prepend a rowto the top level. iter
will be changed to point to this new row. The rowwill be empty after this function is called. To fill in values, you need tocall Gtk.TreeStore:set()
or Gtk.TreeStore:set_value()
.
append()
append (self, parent:Gtk.TreeIter=None) -> iter:Gtk.TreeIter
Appends a new row to tree_store
. If parent
is non-None
, then it will append thenew row after the last child of parent
, otherwise it will append a row tothe top level. iter
will be changed to point to this new row. The row willbe empty after this function is called. To fill in values, you need to callGtk.TreeStore:set()
or Gtk.TreeStore:set_value()
.
is_ancestor()
is_ancestor (self, iter:Gtk.TreeIter, descendant:Gtk.TreeIter) -> bool
Returns True
if iter
is an ancestor of descendant
. That is, iter
is theparent (or grandparent or great-grandparent) of descendant
.
- Returns:
True
, ifiter
is an ancestor ofdescendant
iter_depth()
iter_depth (self, iter:Gtk.TreeIter) -> int
Returns the depth of iter
. This will be 0 for anything on the root level, 1for anything down a level, etc.
- Returns:
The depth of
iter
clear()
clear (self)
Removes all rows from tree_store
iter_is_valid()
iter_is_valid (self, iter:Gtk.TreeIter) -> bool
WARNING: This function is slow. Only use it for debugging and/or testingpurposes.
Checks if the given iter is a valid iter for this Gtk.TreeStore
.
Returns:
True
if the iter is valid,FALSE
if the iter is invalid.Since: 2.2
swap()
swap (self, a:Gtk.TreeIter, b:Gtk.TreeIter)
Swaps a
and b
in the same level of tree_store
. Note that this functiononly works with unsorted stores.
- Since: 2.2
move_before()
move_before (self, iter:Gtk.TreeIter, position:Gtk.TreeIter=None)
Moves iter
in tree_store
to the position before position
. iter
andposition
should be in the same level. Note that this function onlyworks with unsorted stores. If position
is None
, iter
will bemoved to the end of the level.
- Since: 2.2
move_after()
move_after (self, iter:Gtk.TreeIter, position:Gtk.TreeIter=None)
Moves iter
in tree_store
to the position after position
. iter
andposition
should be in the same level. Note that this function onlyworks with unsorted stores. If position
is None
, iter
will be movedto the start of the level.
- Since: 2.2
Example:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class TreeStore(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, -1)
self.connect("destroy", Gtk.main_quit)
treestore = Gtk.TreeStore(str)
dog = treestore.append(None, ["Dog"])
treestore.append(dog, ["Fido"])
treestore.append(dog, ["Spot"])
cat = treestore.append(None, ["Cat"])
treestore.append(cat, ["Ginger"])
rabbit = treestore.append(None, ["Rabbit"])
treestore.append(rabbit, ["Twitch"])
treestore.append(rabbit, ["Floppy"])
treeview = Gtk.TreeView()
treeview.set_model(treestore)
self.add(treeview)
cellrenderertext = Gtk.CellRendererText()
treeviewcolumn = Gtk.TreeViewColumn("Pet Names")
treeview.append_column(treeviewcolumn)
treeviewcolumn.pack_start(cellrenderertext, True)
treeviewcolumn.add_attribute(cellrenderertext, "text", 0)
window = TreeStore()
window.show_all()
Gtk.main()