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



commit 48a60c215940372b9846ccc2183b2bf8cf32943e
Author: Marta Maria Casetti <mmcasetti gmail com>
Date:   Sat Jun 2 16:20:51 2012 +0100

    tutorials python: example of combobox

 platform-demos/C/combobox.py.page    |   34 ++++++++++++++++++++
 platform-demos/C/samples/combobox.py |   58 ++++++++++++++++++++++++++++++++++
 platform-demos/Makefile.am           |    2 +
 3 files changed, 94 insertions(+), 0 deletions(-)
---
diff --git a/platform-demos/C/combobox.py.page b/platform-demos/C/combobox.py.page
new file mode 100644
index 0000000..f8aa193
--- /dev/null
+++ b/platform-demos/C/combobox.py.page
@@ -0,0 +1,34 @@
+<?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.py">
+  <info>
+    <link type="guide" xref="beginner.py#menu-combo-toolbar"/>
+    <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 used to choose from a list of items</desc>
+  </info>
+
+  <title>ComboBox</title>
+  <media type="image" mime="image/png" src="media/combobox.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.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.CellLayout.html";>GtkCellLayout</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/samples/combobox.py b/platform-demos/C/samples/combobox.py
new file mode 100644
index 0000000..e86044f
--- /dev/null
+++ b/platform-demos/C/samples/combobox.py
@@ -0,0 +1,58 @@
+from gi.repository import Gtk
+import sys
+
+distros = [["Select distribution"], ["Fedora"], ["Mint"], ["Suse"]]
+
+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)
+        # there is no insert_with_values in Python, we use append to
+        for i in range(len(distros)):
+            listmodel.append(distros[i])
+
+        # a combobox to see the data stored in the model
+        combobox = Gtk.ComboBox(model=listmodel)
+
+        # a cellrenderer
+        cell = Gtk.CellRendererText()
+        # packs the cell into the beginning of the combobox, allocating
+        # (False) no more space than needed
+        combobox.pack_start(cell, False)
+        # there is no set_attributes() in Python
+        # associates a property ("text") of the cellrenderer (cell) to a column (column 0)
+        # in the model used by the combobox
+        combobox.add_attribute(cell, "text", 0)
+        # 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 its value on the terminal
+        if combo.get_active() != 0:
+			print "You chose " + str(distros[combo.get_active()][0]) +"\n"
+        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/Makefile.am b/platform-demos/Makefile.am
index 75a24bb..d59e745 100644
--- a/platform-demos/Makefile.am
+++ b/platform-demos/Makefile.am
@@ -31,6 +31,7 @@ demo_sources = \
 	samples/checkbutton.c			\
 	samples/checkbutton.py			\
 	samples/checkbutton.vala		\
+	samples/combobox.py			\
 	samples/combobox.vala			\
 	samples/dialog.js			\
 	samples/dialog.py			\
@@ -154,6 +155,7 @@ DOC_PAGES =				\
 	checkbutton.c.page		\
 	checkbutton.py.page		\
 	checkbutton.vala.page		\
+	combobox.py.page		\
 	combobox.vala.page		\
 	cpp.page			\
 	desktop.js.page			\



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