|
Packit |
1470ea |
|
|
Packit |
1470ea |
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="treeview_treestore.py" xml:lang="ca">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">TreeView with TreeStore (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#treeview"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="model-view-controller.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="model-view-controller.py"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-06-30" status="draft"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Marta Maria Casetti</name>
|
|
Packit |
1470ea |
<email its:translate="no">mmcasetti@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<desc>A TreeView displaying a TreeStore (simpler example)</desc>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Simpler TreeView with TreeStore</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/treeview_treestore.png"/>
|
|
Packit |
1470ea |
This TreeView displays a TreeStore.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Code used to generate this example</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
from gi.repository import Pango
|
|
Packit |
1470ea |
import sys
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
books = [["Tolstoy, Leo", "War and Peace", "Anna Karenina"],
|
|
Packit |
1470ea |
["Shakespeare, William", "Hamlet", "Macbeth", "Othello"],
|
|
Packit |
1470ea |
["Tolkien, J.R.R.", "The Lord of the Rings"]]
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyWindow(Gtk.ApplicationWindow):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self, app):
|
|
Packit |
1470ea |
Gtk.Window.__init__(self, title="Library", application=app)
|
|
Packit |
1470ea |
self.set_default_size(250, 100)
|
|
Packit |
1470ea |
self.set_border_width(10)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the data are stored in the model
|
|
Packit |
1470ea |
# create a treestore with one column
|
|
Packit |
1470ea |
store = Gtk.TreeStore(str)
|
|
Packit |
1470ea |
for i in range(len(books)):
|
|
Packit |
1470ea |
# the iter piter is returned when appending the author
|
|
Packit |
1470ea |
piter = store.append(None, [books[i][0]])
|
|
Packit |
1470ea |
# append the books as children of the author
|
|
Packit |
1470ea |
j = 1
|
|
Packit |
1470ea |
while j < len(books[i]):
|
|
Packit |
1470ea |
store.append(piter, [books[i][j]])
|
|
Packit |
1470ea |
j += 1
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the treeview shows the model
|
|
Packit |
1470ea |
# create a treeview on the model store
|
|
Packit |
1470ea |
view = Gtk.TreeView()
|
|
Packit |
1470ea |
view.set_model(store)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the cellrenderer for the column - text
|
|
Packit |
1470ea |
renderer_books = Gtk.CellRendererText()
|
|
Packit |
1470ea |
# the column is created
|
|
Packit |
1470ea |
column_books = Gtk.TreeViewColumn(
|
|
Packit |
1470ea |
"Books by Author", renderer_books, text=0)
|
|
Packit |
1470ea |
# and it is appended to the treeview
|
|
Packit |
1470ea |
view.append_column(column_books)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the books are sortable by author
|
|
Packit |
1470ea |
column_books.set_sort_column_id(0)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# add the treeview to the window
|
|
Packit |
1470ea |
self.add(view)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class MyApplication(Gtk.Application):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def __init__(self):
|
|
Packit |
1470ea |
Gtk.Application.__init__(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_activate(self):
|
|
Packit |
1470ea |
win = MyWindow(self)
|
|
Packit |
1470ea |
win.show_all()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_startup(self):
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="methods">
|
|
Packit |
1470ea |
<title>Useful methods for a TreeView widget</title>
|
|
Packit |
1470ea |
The TreeView widget is designed around a Model/View/Controller design: the Model stores the data; the View gets change notifications and displays the content of the model; the Controller, finally, changes the state of the model and notifies the view of these changes. For more information and for a list of useful methods for TreeModel see <link xref="model-view-controller.py"/>.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>API References</title>
|
|
Packit |
1470ea |
In this sample we used the following:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkTreeView.html">GtkTreeView</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkTreeModel.html">GtkTreeModel</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkTreeStore.html">GtkTreeStore</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkCellRendererText.html">GtkCellRendererText</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkTreeViewColumn.html">GtkTreeViewColumn</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|