|
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="cs">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">TreeView s 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>TreeView zobrazující TreeStore (jednodušší příklad)</desc>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Jednoduchý TreeView s TreeStore</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/treeview_treestore.png"/>
|
|
Packit |
1470ea |
Tento TreeView zobrazuje TreeStore.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Kód použitý k vygenerování tohoto příkladu</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 |
# Data jsou uchována v modelu
|
|
Packit |
1470ea |
# Vytvoří TreeStore s jedním sloupcem
|
|
Packit |
1470ea |
store = Gtk.TreeStore(str)
|
|
Packit |
1470ea |
for i in range(len(books)):
|
|
Packit |
1470ea |
# Iterátor piter je vrácen, když je přidán autor
|
|
Packit |
1470ea |
piter = store.append(None, [books[i][0]])
|
|
Packit |
1470ea |
# Přidá knihu jako potomka k autorovi
|
|
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 |
# TreeView zobrazuje model
|
|
Packit |
1470ea |
# Vytvoří TreeView založené na modelu
|
|
Packit |
1470ea |
view = Gtk.TreeView()
|
|
Packit |
1470ea |
view.set_model(store)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Vykreslovač pro sloupec - text
|
|
Packit |
1470ea |
renderer_books = Gtk.CellRendererText()
|
|
Packit |
1470ea |
# Vytvoří sloupec
|
|
Packit |
1470ea |
column_books = Gtk.TreeViewColumn(
|
|
Packit |
1470ea |
"Books by Author", renderer_books, text=0)
|
|
Packit |
1470ea |
# a přidá jej do TreeView
|
|
Packit |
1470ea |
view.append_column(column_books)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Knihy lze řadit podle autorů
|
|
Packit |
1470ea |
column_books.set_sort_column_id(0)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Přidá stromové zobrazení do okna
|
|
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>Užitečné metody pro widget TreeView</title>
|
|
Packit |
1470ea |
Widget TreeView je navrže okolo návrhu Model/Zobrazení/Ovládání: Model uchovává data, Zobrazení získává upozornění a zobrazuje obsah modelu a konečně Ovládání změní stav modelu a upozorňuje zobrazení na tyto změny. Podrobnější informace a seznam užitečných metod pro stromové zobrazení najdete v <link xref="model-view-controller.py"/>.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>Odkazy k API</title>
|
|
Packit |
1470ea |
V této ukázce se používá následující:
|
|
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>
|