[gnome-devel-docs] tutorials python: example of treeview with simple listview
- From: Tiffany Antopolski <antopolski src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-devel-docs] tutorials python: example of treeview with simple listview
- Date: Tue, 5 Jun 2012 19:35:36 +0000 (UTC)
commit 08e983e499af5ecc19e73ad68eae7f9dc2f63385
Author: Marta Maria Casetti <mmcasetti gmail com>
Date: Sat Jun 2 16:04:40 2012 +0100
tutorials python: example of treeview with simple listview
.../C/samples/treeview_simple_liststore.py | 79 ++++++++++++++++++++
platform-demos/C/treeview_simple_liststore.py.page | 36 +++++++++
platform-demos/Makefile.am | 2 +
3 files changed, 117 insertions(+), 0 deletions(-)
---
diff --git a/platform-demos/C/samples/treeview_simple_liststore.py b/platform-demos/C/samples/treeview_simple_liststore.py
new file mode 100644
index 0000000..820a8ed
--- /dev/null
+++ b/platform-demos/C/samples/treeview_simple_liststore.py
@@ -0,0 +1,79 @@
+from gi.repository import Gtk
+from gi.repository import Pango
+import sys
+
+columns = ["First Name",
+ "Last Name",
+ "Phone Number"]
+
+phonebook = [["Jurg", "Billeter", "555-0123"],
+ ["Johannes", "Schmid", "555-1234"],
+ ["Julita", "Inca", "555-2345"],
+ ["Javier", "Jardon", "555-3456"],
+ ["Jason", "Clinton", "555-4567"],
+ ["Random J.", "Hacker", "555-5678"]]
+
+class MyWindow(Gtk.ApplicationWindow):
+ def __init__(self, app):
+ Gtk.Window.__init__(self, title="My Phone Book", application=app)
+ self.set_default_size(250, 100)
+ self.set_border_width(10)
+
+ # the data in the model (three strings for each row, one for each column)
+ listmodel = Gtk.ListStore(str, str, str)
+ # there is no insert_with_values() in Python, we use append
+ for i in range(len(phonebook)):
+ listmodel.append(phonebook[i])
+
+ # a treeview to see the data stored in the model
+ view = Gtk.TreeView(model=listmodel)
+ # with columns - arguments of treeviewcolumn are:
+ # column title, cell renderer, cell attributes
+ for i in range(len(columns)):
+ cell = Gtk.CellRendererText()
+ # the text in the first column should be in boldface
+ if i == 0:
+ cell.props.weight_set=True
+ cell.props.weight=Pango.Weight.BOLD
+ col = Gtk.TreeViewColumn(columns[i],
+ cell,
+ text=i)
+ view.append_column(col)
+
+
+ # when a row is selected, emit a signal
+ view.get_selection().connect("changed", self.on_changed)
+
+ # the label we use to show the selection
+ self.label = Gtk.Label()
+ self.label.set_text("")
+
+ # a grid to attach the widgets
+ grid = Gtk.Grid()
+ grid.attach(view, 0, 0, 1, 1);
+ grid.attach(self.label, 0, 1, 1, 1);
+
+ self.add(grid)
+
+ def on_changed(self, selection):
+ # get the model and the iterator that points at the data in the model
+ (model, iter) = selection.get_selected()
+ # set the label to a new value depending on the selection
+ self.label.set_text("\n %s %s %s" %(model[iter][0], model[iter][1], model[iter][2]))
+ return True
+
+
+class MyApplication(Gtk.Application):
+ def __init__(self):
+ Gtk.Application.__init__(self, application_id="org.example.treeview_simple_liststore")
+
+ def do_activate(self):
+ win = MyWindow(self)
+ win.show_all()
+
+ def do_startup(self):
+ Gtk.Application.do_startup(self)
+
+app = MyApplication()
+exit_status = app.run(sys.argv)
+sys.exit(exit_status)
diff --git a/platform-demos/C/treeview_simple_liststore.py.page b/platform-demos/C/treeview_simple_liststore.py.page
new file mode 100644
index 0000000..92339a5
--- /dev/null
+++ b/platform-demos/C/treeview_simple_liststore.py.page
@@ -0,0 +1,36 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<page xmlns="http://projectmallard.org/1.0/"
+ xmlns:xi="http://www.w3.org/2001/XInclude"
+ type="guide" style="task"
+ id="treeview_simple_liststore.py">
+ <info>
+ <link type="guide" xref="beginner.py#treeview"/>
+ <link type="seealso" xref="grid.vala"/>
+ <link type="seealso" xref="label.vala"/>
+ <revision version="0.1" date="2012-06-02" status="draft"/>
+
+ <credit type="author copyright">
+ <name>Marta Maria Casetti</name>
+ <email>mmcasetti gmail com</email>
+ <years>2012</years>
+ </credit>
+
+ <desc>A widget can display any TreeModel implementation (lists and trees)</desc>
+ </info>
+
+ <title>Simple Treeview with ListStore</title>
+ <media type="image" mime="image/png" src="media/treeview_simple_liststore.png"/>
+ <p>This TreeView displays a simple ListStore with the Selection "changed" signal connected.</p>
+
+<code mime="text/x-python" style="numbered"><xi:include href="samples/treeview_simple_liststore.py" parse="text"><xi:fallback/></xi:include></code>
+<p>
+ In this sample we used the following:
+</p>
+<list>
+ <item><p><link href="http://developer.gnome.org/gtk3/3.4/GtkTreeView.html">GtkTreeView</link></p></item>
+ <item><p><link href="http://developer.gnome.org/gtk3/3.4/GtkListStore.html">GtkListStore</link></p></item>
+ <item><p><link href="http://developer.gnome.org/gtk3/3.4/GtkTreeViewColumn.html">GtkTreeViewColumn</link></p></item>
+ <item><p><link href="http://git.gnome.org/browse/pygobject/tree/gi/overrides/Gtk.py">pygobject - Python bindings for GObject Introspection</link></p></item>
+ <item><p><link href="http://developer.gnome.org/pango/stable/pango-Fonts.html">Fonts</link></p></item>
+</list>
+</page>
diff --git a/platform-demos/Makefile.am b/platform-demos/Makefile.am
index 65c6191..75a24bb 100644
--- a/platform-demos/Makefile.am
+++ b/platform-demos/Makefile.am
@@ -81,6 +81,7 @@ demo_sources = \
samples/togglebutton.vala \
samples/toolbar.js \
samples/toolbar.vala \
+ samples/treeview_simple_liststore.py \
samples/treeview_simple_liststore.vala \
samples/window.c \
samples/window.js \
@@ -226,6 +227,7 @@ DOC_PAGES = \
togglebutton.vala.page \
toolbar.js.page \
toolbar.vala.page \
+ treeview_simple_liststore.py.page \
treeview_simple_liststore.vala.page \
translate.page \
vala.page \
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]