|
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_cellrenderertoggle.py" xml:lang="de">
|
|
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="next" xref="widget_drawing.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 (more complex example, with CellRendererToggle)</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Mario Blättermann</mal:name>
|
|
Packit |
1470ea |
<mal:email>mario.blaettermann@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011, 2013</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>More Complex TreeView with TreeStore</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/treeview_cellrenderertoggle.png"/>
|
|
Packit |
1470ea |
This TreeView displays a TreeStore with two columns, one of which is rendered as a toggle.
|
|
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", True], ["Anna Karenina", False]],
|
|
Packit |
1470ea |
["Shakespeare, William", ["Hamlet", False],
|
|
Packit |
1470ea |
["Macbeth", True], ["Othello", False]],
|
|
Packit |
1470ea |
["Tolkien, J.R.R.", ["The Lord of the Rings", False]]]
|
|
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 two columns
|
|
Packit |
1470ea |
self.store = Gtk.TreeStore(str, bool)
|
|
Packit |
1470ea |
# fill in the model
|
|
Packit |
1470ea |
for i in range(len(books)):
|
|
Packit |
1470ea |
# the iter piter is returned when appending the author in the first column
|
|
Packit |
1470ea |
# and False in the second
|
|
Packit |
1470ea |
piter = self.store.append(None, [books[i][0], False])
|
|
Packit |
1470ea |
# append the books and the associated boolean value as children of
|
|
Packit |
1470ea |
# the author
|
|
Packit |
1470ea |
j = 1
|
|
Packit |
1470ea |
while j < len(books[i]):
|
|
Packit |
1470ea |
self.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 self.store
|
|
Packit |
1470ea |
view = Gtk.TreeView()
|
|
Packit |
1470ea |
view.set_model(self.store)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the cellrenderer for the first column - text
|
|
Packit |
1470ea |
renderer_books = Gtk.CellRendererText()
|
|
Packit |
1470ea |
# the first column is created
|
|
Packit |
1470ea |
column_books = Gtk.TreeViewColumn("Books", 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 cellrenderer for the second column - boolean rendered as a toggle
|
|
Packit |
1470ea |
renderer_in_out = Gtk.CellRendererToggle()
|
|
Packit |
1470ea |
# the second column is created
|
|
Packit |
1470ea |
column_in_out = Gtk.TreeViewColumn("Out?", renderer_in_out, active=1)
|
|
Packit |
1470ea |
# and it is appended to the treeview
|
|
Packit |
1470ea |
view.append_column(column_in_out)
|
|
Packit |
1470ea |
# connect the cellrenderertoggle with a callback function
|
|
Packit |
1470ea |
renderer_in_out.connect("toggled", self.on_toggled)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# add the treeview to the window
|
|
Packit |
1470ea |
self.add(view)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for the signal emitted by the cellrenderertoggle
|
|
Packit |
1470ea |
def on_toggled(self, widget, path):
|
|
Packit |
1470ea |
# the boolean value of the selected row
|
|
Packit |
1470ea |
current_value = self.store[path][1]
|
|
Packit |
1470ea |
# change the boolean value of the selected row in the model
|
|
Packit |
1470ea |
self.store[path][1] = not current_value
|
|
Packit |
1470ea |
# new current value!
|
|
Packit |
1470ea |
current_value = not current_value
|
|
Packit |
1470ea |
# if length of the path is 1 (that is, if we are selecting an author)
|
|
Packit |
1470ea |
if len(path) == 1:
|
|
Packit |
1470ea |
# get the iter associated with the path
|
|
Packit |
1470ea |
piter = self.store.get_iter(path)
|
|
Packit |
1470ea |
# get the iter associated with its first child
|
|
Packit |
1470ea |
citer = self.store.iter_children(piter)
|
|
Packit |
1470ea |
# while there are children, change the state of their boolean value
|
|
Packit |
1470ea |
# to the value of the author
|
|
Packit |
1470ea |
while citer is not None:
|
|
Packit |
1470ea |
self.store[citer][1] = current_value
|
|
Packit |
1470ea |
citer = self.store.iter_next(citer)
|
|
Packit |
1470ea |
# if the length of the path is not 1 (that is, if we are selecting a
|
|
Packit |
1470ea |
# book)
|
|
Packit |
1470ea |
elif len(path) != 1:
|
|
Packit |
1470ea |
# get the first child of the parent of the book (the first book of
|
|
Packit |
1470ea |
# the author)
|
|
Packit |
1470ea |
citer = self.store.get_iter(path)
|
|
Packit |
1470ea |
piter = self.store.iter_parent(citer)
|
|
Packit |
1470ea |
citer = self.store.iter_children(piter)
|
|
Packit |
1470ea |
# check if all the children are selected
|
|
Packit |
1470ea |
all_selected = True
|
|
Packit |
1470ea |
while citer is not None:
|
|
Packit |
1470ea |
if self.store[citer][1] == False:
|
|
Packit |
1470ea |
all_selected = False
|
|
Packit |
1470ea |
break
|
|
Packit |
1470ea |
citer = self.store.iter_next(citer)
|
|
Packit |
1470ea |
# if they do, the author as well is selected; otherwise it is not
|
|
Packit |
1470ea |
self.store[piter][1] = all_selected
|
|
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 |
In line 48 the "toggled" signal is connected to the callback function on_toggled() using widget.connect(signal, callback function) . See <link xref="signals-callbacks.py"/> for a more detailed explanation.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>API-Referenzen</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/GtkCellRendererToggle.html">GtkCellRendererToggle</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>
|