[gnome-devel-docs] tutorials python: example of multicolumn combobox



commit ed57ad7260f6f69fb651c4b058cce83b6d10cb8c
Author: Marta Maria Casetti <mmcasetti gmail com>
Date:   Sun Jun 3 10:03:19 2012 +0100

    tutorials python: example of multicolumn combobox

 platform-demos/C/combobox_multicolumn.py.page    |   36 +++++++++++
 platform-demos/C/media/combobox_multicolumn.png  |  Bin 0 -> 7387 bytes
 platform-demos/C/samples/combobox_multicolumn.py |   69 ++++++++++++++++++++++
 platform-demos/Makefile.am                       |    3 +
 4 files changed, 108 insertions(+), 0 deletions(-)
---
diff --git a/platform-demos/C/combobox_multicolumn.py.page b/platform-demos/C/combobox_multicolumn.py.page
new file mode 100644
index 0000000..7f7320d
--- /dev/null
+++ b/platform-demos/C/combobox_multicolumn.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="combobox_multicolumn.py">
+  <info>
+    <link type="guide" xref="beginner.py#menu-combo-toolbar"/>
+    <link type="seealso" xref="combobox.py"/>
+    <revision version="0.1" date="2012-06-03" status="draft"/>
+
+    <credit type="author copyright">
+      <name>Marta Maria Casetti</name>
+      <email>mmcasetti gmail com</email>
+      <years>2012</years>
+    </credit>
+
+    <desc>A widget used to choose from a list of items</desc>
+  </info>
+
+  <title>ComboBox</title>
+  <media type="image" mime="image/png" src="media/combobox_multicolumn.png"/>
+  <p>This ComboBox prints to the terminal your selection when you change it.</p>
+
+<code mime="text/x-python" style="numbered"><xi:include href="samples/combobox_multicolumn.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/GtkComboBox.html";>GtkComboBox</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/Gtk.CellRendererText.html";>GtkCellRendererText</link></p></item>
+  <item><p><link href="http://developer.gnome.org/gtk3/3.4/Gtk.CellRendererPixbuf.html";>GtkCellRendererPixbuf</link></p></item>
+  <item><p><link href="http://developer.gnome.org/gtk3/3.4/gtk3-Stock_Items.html";>Stock Items</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>
+</list>
+</page>
diff --git a/platform-demos/C/media/combobox_multicolumn.png b/platform-demos/C/media/combobox_multicolumn.png
new file mode 100644
index 0000000..7529311
Binary files /dev/null and b/platform-demos/C/media/combobox_multicolumn.png differ
diff --git a/platform-demos/C/samples/combobox_multicolumn.py b/platform-demos/C/samples/combobox_multicolumn.py
new file mode 100644
index 0000000..2e59fcd
--- /dev/null
+++ b/platform-demos/C/samples/combobox_multicolumn.py
@@ -0,0 +1,69 @@
+from gi.repository import Gtk
+import sys
+
+actions = [["Select", None],
+           ["New", Gtk.STOCK_NEW],
+           ["Open", Gtk.STOCK_OPEN],
+           ["Save", Gtk.STOCK_SAVE]]
+
+class MyWindow(Gtk.ApplicationWindow):
+    def __init__(self, app):
+        Gtk.Window.__init__(self, title="Welcome to GNOME", application=app)
+        self.set_default_size(200, -1)
+        self.set_border_width(10)
+
+        # the data in the model, of type string
+        listmodel = Gtk.ListStore(str, str)
+        # there is no insert_with_values in Python, we use append
+        for i in range(len(actions)):
+            listmodel.append(actions[i])
+
+        # a combobox to see the data stored in the model
+        combobox = Gtk.ComboBox(model=listmodel)
+
+        # cellrenderers
+        renderer_pixbuf = Gtk.CellRendererPixbuf()
+        renderer_text = Gtk.CellRendererText()
+        # we pack the cell into the beginning of the combobox, allocating
+        # (False) no more space than needed
+
+        # first the image, then the text
+        # it does not matter in which order they are in the model,
+        # the visualization is decided by the order of the cellrenderers
+        combobox.pack_start(renderer_pixbuf, False)
+        combobox.pack_start(renderer_text, False)
+
+        # there is no set_attributes() in Python
+        # associates a property of the cellrenderer to a column in the model
+        # used by the combobox
+        combobox.add_attribute(renderer_text, "text", 0)
+        combobox.add_attribute(renderer_pixbuf, "stock_id", 1)
+        # the first row is the active one
+        combobox.set_active(0)
+
+        # when a row is selected, emit a signal
+        combobox.connect("changed", self.on_changed)
+
+        self.add(combobox)
+
+    def on_changed(self, combo):
+        # if the row selected is not the first one, write on the terminal
+        if combo.get_active() != 0:
+			print "You chose " + str(actions[combo.get_active()][0]) +"\n"
+        return True
+
+
+class MyApplication(Gtk.Application):
+    def __init__(self):
+        Gtk.Application.__init__(self, application_id="org.example.combobox_multicolumn")
+
+    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/Makefile.am b/platform-demos/Makefile.am
index d59e745..30048bb 100644
--- a/platform-demos/Makefile.am
+++ b/platform-demos/Makefile.am
@@ -33,6 +33,7 @@ demo_sources = \
 	samples/checkbutton.vala		\
 	samples/combobox.py			\
 	samples/combobox.vala			\
+	samples/combobox_multicolumn.vala			\
 	samples/dialog.js			\
 	samples/dialog.py			\
 	samples/dialog.vala			\
@@ -95,6 +96,7 @@ DOC_FIGURES = \
 	media/button.png			\
 	media/checkbutton.png			\
 	media/combobox.png			\
+	media/combobox_multicolumn.png			\
 	media/dialog.png			\
 	media/entry.png				\
 	media/fedora.png			\
@@ -157,6 +159,7 @@ DOC_PAGES =				\
 	checkbutton.vala.page		\
 	combobox.py.page		\
 	combobox.vala.page		\
+	combobox_multicolumn.vala.page		\
 	cpp.page			\
 	desktop.js.page			\
 	dialog.js.page			\



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]