Blame platform-demos/fr/combobox.py.page

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="combobox.py" xml:lang="fr">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">ComboBox (Python)</title>
Packit 1470ea
    <link type="guide" xref="beginner.py#menu-combo-toolbar"/>
Packit 1470ea
    <link type="seealso" xref="model-view-controller.py"/>
Packit 1470ea
    <link type="next" xref="treeview_simple_liststore.py"/>
Packit 1470ea
    <revision version="0.1" date="2012-06-02" 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>Un composant graphique utilisé pour effectuer un choix dans une liste d'éléments</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Luc Rebert,</mal:name>
Packit 1470ea
      <mal:email>traduc@rebert.name</mal:email>
Packit 1470ea
      <mal:years>2011</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Alain Lojewski,</mal:name>
Packit 1470ea
      <mal:email>allomervan@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011-2012</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Luc Pionchon</mal:name>
Packit 1470ea
      <mal:email>pionchon.luc@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Bruno Brouard</mal:name>
Packit 1470ea
      <mal:email>annoa.b@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011-12</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Luis Menina</mal:name>
Packit 1470ea
      <mal:email>liberforce@freeside.fr</mal:email>
Packit 1470ea
      <mal:years>2014</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>ComboBox (one column)</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/combobox.png"/>
Packit 1470ea
  

Cette BoiteCombinee affiche votre sélection dans le terminal quand vous la modifiez.

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>Code utilisé pour générer cet exemple</title>
Packit 1470ea
    from gi.repository import Gtk
Packit 1470ea
import sys
Packit 1470ea
Packit 1470ea
distros = [["Select distribution"], ["Fedora"], ["Mint"], ["Suse"]]
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="Welcome to GNOME", application=app)
Packit 1470ea
        self.set_default_size(200, -1)
Packit 1470ea
        self.set_border_width(10)
Packit 1470ea
Packit 1470ea
        # the data in the model, of type string
Packit 1470ea
        listmodel = Gtk.ListStore(str)
Packit 1470ea
        # append the data in the model
Packit 1470ea
        for i in range(len(distros)):
Packit 1470ea
            listmodel.append(distros[i])
Packit 1470ea
Packit 1470ea
        # a combobox to see the data stored in the model
Packit 1470ea
        combobox = Gtk.ComboBox(model=listmodel)
Packit 1470ea
Packit 1470ea
        # a cellrenderer to render the text
Packit 1470ea
        cell = Gtk.CellRendererText()
Packit 1470ea
Packit 1470ea
        # pack the cell into the beginning of the combobox, allocating
Packit 1470ea
        # no more space than needed
Packit 1470ea
        combobox.pack_start(cell, False)
Packit 1470ea
        # associate a property ("text") of the cellrenderer (cell) to a column (column 0)
Packit 1470ea
        # in the model used by the combobox
Packit 1470ea
        combobox.add_attribute(cell, "text", 0)
Packit 1470ea
Packit 1470ea
        # the first row is the active one by default at the beginning
Packit 1470ea
        combobox.set_active(0)
Packit 1470ea
Packit 1470ea
        # connect the signal emitted when a row is selected to the callback
Packit 1470ea
        # function
Packit 1470ea
        combobox.connect("changed", self.on_changed)
Packit 1470ea
Packit 1470ea
        # add the combobox to the window
Packit 1470ea
        self.add(combobox)
Packit 1470ea
Packit 1470ea
    def on_changed(self, combo):
Packit 1470ea
        # if the row selected is not the first one, write its value on the
Packit 1470ea
        # terminal
Packit 1470ea
        if combo.get_active() != 0:
Packit 1470ea
            print("You chose " + str(distros[combo.get_active()][0]) + ".")
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>Méthode utiles pour un élément graphique BoiteCombinee</title>
Packit 1470ea
    

The ComboBox widget is designed around a Model/View/Controller design: the Model stores the data; the View gets change notifications and displays the content of the model; the Controller, finally, changes the state of the model and notifies the view of these changes. For more information and for a list of useful methods for ComboBox see <link xref="model-view-controller.py"/>.

Packit 1470ea
    

In line 35 the "changed" signal is connected to the callback function on_changed() using widget.connect(signal, callback function). See <link xref="signals-callbacks.py"/> for a more detailed explanation.

Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
    <title>Références API</title>
Packit 1470ea
    

Dans cet exemple, les éléments suivants sont utilisés :

Packit 1470ea
    <list>
Packit 1470ea
      <item>

<link href="http://developer.gnome.org/gtk3/unstable/GtkComboBox.html">GtkComboBox</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/GtkCellLayout.html">GtkCellLayout</link>

</item>
Packit 1470ea
      <item>

<link href="http://git.gnome.org/browse/pygobject/tree/gi/overrides/Gtk.py">pygobject - Python bindings for GObject Introspection</link>

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
</page>