|
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_simple_liststore.py" xml:lang="cs">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Jednoduchý TreeView s ListStore (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="treeview_treestore.py"/>
|
|
Packit |
1470ea |
<revision version="0.2" 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í ListStore (jednodušší příklad)</desc>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Jednoduchý TreeView s ListStore</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/treeview_simple_liststore.png"/>
|
|
Packit |
1470ea |
Tento TreeView zobrazuje jednoduchý ListStore s napojeným signálem "changed" (změna výběru).
|
|
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 |
columns = ["First Name",
|
|
Packit |
1470ea |
"Last Name",
|
|
Packit |
1470ea |
"Phone Number"]
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
phonebook = [["Jurg", "Billeter", "555-0123"],
|
|
Packit |
1470ea |
["Johannes", "Schmid", "555-1234"],
|
|
Packit |
1470ea |
["Julita", "Inca", "555-2345"],
|
|
Packit |
1470ea |
["Javier", "Jardon", "555-3456"],
|
|
Packit |
1470ea |
["Jason", "Clinton", "555-4567"],
|
|
Packit |
1470ea |
["Random J.", "Hacker", "555-5678"]]
|
|
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="My Phone Book", application=app)
|
|
Packit |
1470ea |
self.set_default_size(250, 100)
|
|
Packit |
1470ea |
self.set_border_width(10)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Datový model (tři řetězce pro každý řádek, jeden na každý
|
|
Packit |
1470ea |
# slupec)
|
|
Packit |
1470ea |
listmodel = Gtk.ListStore(str, str, str)
|
|
Packit |
1470ea |
# Nasype do modelu hodnoty
|
|
Packit |
1470ea |
for i in range(len(phonebook)):
|
|
Packit |
1470ea |
listmodel.append(phonebook[i])
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Stromové zobrazení pro zobrazení dat uložených v modelu
|
|
Packit |
1470ea |
view = Gtk.TreeView(model=listmodel)
|
|
Packit |
1470ea |
# Pro každý sloupec
|
|
Packit |
1470ea |
for i, column in enumerate(columns):
|
|
Packit |
1470ea |
# Vykreslovač buňky vykreslí text
|
|
Packit |
1470ea |
cell = Gtk.CellRendererText()
|
|
Packit |
1470ea |
# Text v prvním sloupci má být tučný
|
|
Packit |
1470ea |
if i == 0:
|
|
Packit |
1470ea |
cell.props.weight_set = True
|
|
Packit |
1470ea |
cell.props.weight = Pango.Weight.BOLD
|
|
Packit |
1470ea |
# Vytvoří sloupec
|
|
Packit |
1470ea |
col = Gtk.TreeViewColumn(column, cell, text=i)
|
|
Packit |
1470ea |
# Připojí sloupec do stromového zobrazení
|
|
Packit |
1470ea |
view.append_column(col)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Když je vybrán řádek, je vyslán signál
|
|
Packit |
1470ea |
view.get_selection().connect("changed", self.on_changed)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Popisek, který se používá k zobrazení výběru
|
|
Packit |
1470ea |
self.label = Gtk.Label()
|
|
Packit |
1470ea |
self.label.set_text("")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Mřížka pro připojení widgetů
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.attach(view, 0, 0, 1, 1)
|
|
Packit |
1470ea |
grid.attach(self.label, 0, 1, 1, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Připojí mřížku do okna
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def on_changed(self, selection):
|
|
Packit |
1470ea |
# Získá model a iterátor, který ukazuje na data v modelu
|
|
Packit |
1470ea |
(model, iter) = selection.get_selected()
|
|
Packit |
1470ea |
# Nastaví popisek na novou hodnotu podle výběru
|
|
Packit |
1470ea |
self.label.set_text("\n %s %s %s" %
|
|
Packit |
1470ea |
(model[iter][0], model[iter][1], model[iter][2]))
|
|
Packit |
1470ea |
return True
|
|
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 vytvořen okolo návrhu Model/Zobrazení/Ovládání: Model uchovává data, Zobrazení dodává upozornění na změny a zobrazuje obsah modelu a konečně Ovládání mění stav modelu a upozorňuje zobrazení na tyto změny. Více informací a seznam použitelných metod pro TreeModel najdete v <link xref="model-view-controller.py"/>.
|
|
Packit |
1470ea |
Na řádku 44 je signál "changed" napojen na funkci zpětného volání on_changed() pomocí widget.connect(signál, funkce zpětného volání) . Podrobnější vysvětlení viz <link xref="signals-callbacks.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/GtkListStore.html">GtkListStore</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 |
<item><link href="http://git.gnome.org/browse/pygobject/tree/gi/overrides/Gtk.py">pygobject – vazba jazyka Python pro GObject Introspection</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/pango/stable/pango-Fonts.html">Písma</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|