|
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="menubar.py" xml:lang="fr">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">MenuBar (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#menu-combo-toolbar"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="gmenu.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="colorbutton.py"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-08-01" status="stub"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<credit type="author copyright">
|
|
Packit |
1470ea |
<name>Tiffany Antopolski</name>
|
|
Packit |
1470ea |
<email its:translate="no">tiffany.antopolski@gmail.com</email>
|
|
Packit |
1470ea |
<years>2012</years>
|
|
Packit |
1470ea |
</credit>
|
|
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 qui contient des éléments graphiques GtkMenuItem</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>MenuBar created using XML and GtkBuilder</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/menubar.png"/>
|
|
Packit |
1470ea |
Une BarreDeMenu créée avec XML et GtkBuilder.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="xml"> <title>Création d'une BarreDeMenu avec XML</title>
|
|
Packit |
1470ea |
Pour créer une BarreDeMenu avec XML :
|
|
Packit |
1470ea |
<steps>
|
|
Packit |
1470ea |
<item>créez le fichier <file>barredemenu.ui</file> avec votre éditeur de texte favorit. </item>
|
|
Packit |
1470ea |
<item>Saisissez la ligne suivante en haut du fichier :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>We want to create the interface which will contain our menubar and its submenus. Our menubar will contain <gui>File</gui>, <gui>Edit</gui>, <gui>Choices</gui> and <gui>Help</gui> submenus. We add the following XML code to the file:
|
|
Packit |
1470ea |
<?xml version="1.0" encoding="UTF-8"?>
|
|
Packit |
1470ea |
<interface>
|
|
Packit |
1470ea |
<menu id="menubar">
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">File</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Edit</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Choices</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Help</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
</menu>
|
|
Packit |
1470ea |
</interface>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>Maintenant, créons le fichier .py et utilisons GtkBuilder pour importer le fichier <file>barredemenu.ui</file> que nous venons de faire. </item>
|
|
Packit |
1470ea |
</steps>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="basis"> <title>Ajout de la BarreDeMenu à la fenêtre avec GtkBuilder</title>
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
import sys
|
|
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="MenuBar Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(200, 200)
|
|
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 |
# a builder to add the UI designed with Glade to the grid:
|
|
Packit |
1470ea |
builder = Gtk.Builder()
|
|
Packit |
1470ea |
# get the file (if it is there)
|
|
Packit |
1470ea |
try:
|
|
Packit |
1470ea |
builder.add_from_file("menubar_basis.ui")
|
|
Packit |
1470ea |
except:
|
|
Packit |
1470ea |
print("file not found")
|
|
Packit |
1470ea |
sys.exit()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# we use the method Gtk.Application.set_menubar(menubar) to add the menubar
|
|
Packit |
1470ea |
# to the application (Note: NOT the window!)
|
|
Packit |
1470ea |
self.set_menubar(builder.get_object("menubar"))
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
app = MyApplication()
|
|
Packit |
1470ea |
exit_status = app.run(sys.argv)
|
|
Packit |
1470ea |
sys.exit(exit_status)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Démarrez maintenant l'application python. Vous devriez voir quelque chose qui ressemble à l'image en haut de cette page.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="xml2"> <title>Ajout d'éléments aux menus</title>
|
|
Packit |
1470ea |
Commençons par ajouter 2 éléments au menu <gui>Fichier</gui> : <gui>Nouveau</gui> et <gui>Quitter</gui>. Pour ce faire, ajoutons une section contenant ces éléments au SouMenu Fichier . Le fichier <file>barredemenu.ui</file> devrait ressembler à ceci (les lignes 6 à 13 comportent la section nouvellement ajoutée) :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<listing>
|
|
Packit |
1470ea |
<title>menubar.ui</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<interface>
|
|
Packit |
1470ea |
<menu id="menubar">
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">File</attribute>
|
|
Packit |
1470ea |
<section>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">New</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name ="label">Quit</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Edit</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Choices</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Help</attribute>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
</menu>
|
|
Packit |
1470ea |
</interface>]]>
|
|
Packit |
1470ea |
</listing>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
En suivant ce modèle, ajoutez maintenant les éléments Copier et Coller au SousMenu Édition et un élément À propos au SousMenu Aide .
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="actions">
|
|
Packit |
1470ea |
<title>Définition des actions</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Dans le fichier Python, créons les actions « Nouveau » et « Quitter » et connectons-les à une fonction de rappel ; par exemple, créons « new » de cette façon :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
new_action = Gio.SimpleAction.new("new", None)
|
|
Packit |
1470ea |
new_action.connect("activate", self.new_callback)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
et la fonction de rappel de « new » de cette façon :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def new_callback(self, action, parameter):
|
|
Packit |
1470ea |
print "You clicked \"New\""
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Now, in the XML file, we connect the menu items to the actions in the XML file by adding the "action" attribute:
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">New</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.new</attribute>
|
|
Packit |
1470ea |
</item>]]>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Notez que pour une action relative à l'application, nous utilisons le préfixe app. , et que pour une action relative à la fenêtre, nous utilisons le préfixe win. .
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Finalement, nous ajoutons l'action à l'application ou à la fenêtre dans le fichier python - ainsi, par exemple, app.new est ajouté à l'application dans la méthode do_startup(self) de cette façon :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
self.add_action(new_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
See <link xref="signals-callbacks.py"/> for a more detailed explanation of signals and callbacks.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="win-app"><title>Actions : application ou fenêtre ?</title>
|
|
Packit |
1470ea |
Ci-dessus, nous créons les actions « new » et « open » comme faisant partie de la classe MyApplication. Les actions qui gèrent l'application elle-même (comme « quit »), doivent être créées de la même façon.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Quelques actions, comme « copy » et « paste » gèrent la fenêtre, pas l'application. Les actions gérant la fenêtre doivent être créées dans la classe window.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
The complete example files contain both application actions and window actions. The window actions are the ones usually included in the <link xref="gmenu.py">application menu</link> also. It is not good practice to include window actions in the application menu. For demonstration purposes, the complete example files which follow include XML in the UI file which creates the application menu which includes a "New" and "Open" item, and these are hooked up to the same actions as the menubar items of the same name.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="choices">
|
|
Packit |
1470ea |
<title>Le SousMenu Choix et les éléments avec leur état</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/menubar_choices.png"/>
|
|
Packit |
1470ea |
Les lignes 30 à 80 inclue de <link xref="menubar.py#xml-code"/> décrivent le code XML utilisé pour créer le menu <gui>Choix</gui> de l'interface utilisateur.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
les actions créées jusqu'à présent sont sans état (stateless), c-à-d. qu'elles ne prennent pas ni ne dépendent pas d'un état définit par l'action elle-même. Par contre, les actions que nous avons besoin de créer pour le SousMenu Choix sont avec état (stateful). Voici un exemple de création d'une action stateful :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
shape_action = Gio.SimpleAction.new_stateful("shape", GLib.VariantType.new('s'), GLib.Variant.new_string('line'))
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
où les variables de la méthode sont : le nom, le type de paramètre (dans ce cas, une chaîne - voyez <link href="http://developer.gnome.org/glib/unstable/glib-GVariantType.html">ici</link> pour une liste complète des significations des caractères), l'état initial (dans ce cas, « line » - dans le cas d'une valeur boléenne True , il devrait être Glib.Variant.new_boolean(True) et ainsi de suite. Voyez <link href="http://developer.gnome.org/glib/unstable/glib-GVariant.html">ici</link> pour consulter une liste complète)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Après avoir créé l'ActionSimple stateful, nous la connectons à la fonction de rappel et nous l'ajoutons à la fenêtre (ou à l'application selon le cas) comme auparavant :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
shape_action.connect("activate", self.shape_callback)
|
|
Packit |
1470ea |
self.add_action(shape_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="xml-code">
|
|
Packit |
1470ea |
<title>Fichier XML complet de l'interface utilisateur de cet exemple</title>
|
|
Packit |
1470ea |
<?xml version="1.0" encoding="UTF-8"?>
|
|
Packit |
1470ea |
<interface>
|
|
Packit |
1470ea |
<menu id="menubar">
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">File</attribute>
|
|
Packit |
1470ea |
<section>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">New</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.new</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Quit</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.quit</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Edit</attribute>
|
|
Packit |
1470ea |
<section>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Copy</attribute>
|
|
Packit |
1470ea |
<attribute name="action">win.copy</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Paste</attribute>
|
|
Packit |
1470ea |
<attribute name="action">win.paste</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Choices</attribute>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Shapes</attribute>
|
|
Packit |
1470ea |
<section>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Line</attribute>
|
|
Packit |
1470ea |
<attribute name="action">win.shape</attribute>
|
|
Packit |
1470ea |
<attribute name="target">line</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Triangle</attribute>
|
|
Packit |
1470ea |
<attribute name="action">win.shape</attribute>
|
|
Packit |
1470ea |
<attribute name="target">triangle</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Square</attribute>
|
|
Packit |
1470ea |
<attribute name="action">win.shape</attribute>
|
|
Packit |
1470ea |
<attribute name="target">square</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Polygon</attribute>
|
|
Packit |
1470ea |
<attribute name="action">win.shape</attribute>
|
|
Packit |
1470ea |
<attribute name="target">polygon</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Circle</attribute>
|
|
Packit |
1470ea |
<attribute name="action">win.shape</attribute>
|
|
Packit |
1470ea |
<attribute name="target">circle</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<section>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">On</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.state</attribute>
|
|
Packit |
1470ea |
<attribute name="target">on</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Off</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.state</attribute>
|
|
Packit |
1470ea |
<attribute name="target">off</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
<section>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Awesome</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.awesome</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
<submenu>
|
|
Packit |
1470ea |
<attribute name="label">Help</attribute>
|
|
Packit |
1470ea |
<section>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">About</attribute>
|
|
Packit |
1470ea |
<attribute name="action">win.about</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</submenu>
|
|
Packit |
1470ea |
</menu>
|
|
Packit |
1470ea |
<menu id="appmenu">
|
|
Packit |
1470ea |
<section>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">New</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.new</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">Quit</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.quit</attribute>
|
|
Packit |
1470ea |
</item>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</menu>
|
|
Packit |
1470ea |
</interface>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="python-code">
|
|
Packit |
1470ea |
<title>Fichier Python complet de cet exemple</title>
|
|
Packit |
1470ea |
from gi.repository import Gtk
|
|
Packit |
1470ea |
from gi.repository import GLib
|
|
Packit |
1470ea |
from gi.repository import Gio
|
|
Packit |
1470ea |
import sys
|
|
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="MenuBar Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(200, 200)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# action without a state created (name, parameter type)
|
|
Packit |
1470ea |
copy_action = Gio.SimpleAction.new("copy", None)
|
|
Packit |
1470ea |
# connected with the callback function
|
|
Packit |
1470ea |
copy_action.connect("activate", self.copy_callback)
|
|
Packit |
1470ea |
# added to the window
|
|
Packit |
1470ea |
self.add_action(copy_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# action without a state created (name, parameter type)
|
|
Packit |
1470ea |
paste_action = Gio.SimpleAction.new("paste", None)
|
|
Packit |
1470ea |
# connected with the callback function
|
|
Packit |
1470ea |
paste_action.connect("activate", self.paste_callback)
|
|
Packit |
1470ea |
# added to the window
|
|
Packit |
1470ea |
self.add_action(paste_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# action with a state created (name, parameter type, initial state)
|
|
Packit |
1470ea |
shape_action = Gio.SimpleAction.new_stateful(
|
|
Packit |
1470ea |
"shape", GLib.VariantType.new('s'), GLib.Variant.new_string('line'))
|
|
Packit |
1470ea |
# connected to the callback function
|
|
Packit |
1470ea |
shape_action.connect("activate", self.shape_callback)
|
|
Packit |
1470ea |
# added to the window
|
|
Packit |
1470ea |
self.add_action(shape_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# action with a state created
|
|
Packit |
1470ea |
about_action = Gio.SimpleAction.new("about", None)
|
|
Packit |
1470ea |
# action connected to the callback function
|
|
Packit |
1470ea |
about_action.connect("activate", self.about_callback)
|
|
Packit |
1470ea |
# action added to the application
|
|
Packit |
1470ea |
self.add_action(about_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for copy_action
|
|
Packit |
1470ea |
def copy_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("\"Copy\" activated")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for paste_action
|
|
Packit |
1470ea |
def paste_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("\"Paste\" activated")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for shape_action
|
|
Packit |
1470ea |
def shape_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("Shape is set to", parameter.get_string())
|
|
Packit |
1470ea |
# Note that we set the state of the action!
|
|
Packit |
1470ea |
action.set_state(parameter)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for about (see the AboutDialog example)
|
|
Packit |
1470ea |
def about_callback(self, action, parameter):
|
|
Packit |
1470ea |
# a Gtk.AboutDialog
|
|
Packit |
1470ea |
aboutdialog = Gtk.AboutDialog()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# lists of authors and documenters (will be used later)
|
|
Packit |
1470ea |
authors = ["GNOME Documentation Team"]
|
|
Packit |
1470ea |
documenters = ["GNOME Documentation Team"]
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# we fill in the aboutdialog
|
|
Packit |
1470ea |
aboutdialog.set_program_name("MenuBar Example")
|
|
Packit |
1470ea |
aboutdialog.set_copyright(
|
|
Packit |
1470ea |
"Copyright \xc2\xa9 2012 GNOME Documentation Team")
|
|
Packit |
1470ea |
aboutdialog.set_authors(authors)
|
|
Packit |
1470ea |
aboutdialog.set_documenters(documenters)
|
|
Packit |
1470ea |
aboutdialog.set_website("http://developer.gnome.org")
|
|
Packit |
1470ea |
aboutdialog.set_website_label("GNOME Developer Website")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# to close the aboutdialog when "close" is clicked we connect the
|
|
Packit |
1470ea |
# "response" signal to on_close
|
|
Packit |
1470ea |
aboutdialog.connect("response", self.on_close)
|
|
Packit |
1470ea |
# show the aboutdialog
|
|
Packit |
1470ea |
aboutdialog.show()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a callback function to destroy the aboutdialog
|
|
Packit |
1470ea |
def on_close(self, action, parameter):
|
|
Packit |
1470ea |
action.destroy()
|
|
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 |
# FIRST THING TO DO: do_startup()
|
|
Packit |
1470ea |
Gtk.Application.do_startup(self)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# action without a state created
|
|
Packit |
1470ea |
new_action = Gio.SimpleAction.new("new", None)
|
|
Packit |
1470ea |
# action connected to the callback function
|
|
Packit |
1470ea |
new_action.connect("activate", self.new_callback)
|
|
Packit |
1470ea |
# action added to the application
|
|
Packit |
1470ea |
self.add_action(new_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# action without a state created
|
|
Packit |
1470ea |
quit_action = Gio.SimpleAction.new("quit", None)
|
|
Packit |
1470ea |
# action connected to the callback function
|
|
Packit |
1470ea |
quit_action.connect("activate", self.quit_callback)
|
|
Packit |
1470ea |
# action added to the application
|
|
Packit |
1470ea |
self.add_action(quit_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# action with a state created
|
|
Packit |
1470ea |
state_action = Gio.SimpleAction.new_stateful(
|
|
Packit |
1470ea |
"state", GLib.VariantType.new('s'), GLib.Variant.new_string('off'))
|
|
Packit |
1470ea |
# action connected to the callback function
|
|
Packit |
1470ea |
state_action.connect("activate", self.state_callback)
|
|
Packit |
1470ea |
# action added to the application
|
|
Packit |
1470ea |
self.add_action(state_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# action with a state created
|
|
Packit |
1470ea |
awesome_action = Gio.SimpleAction.new_stateful(
|
|
Packit |
1470ea |
"awesome", None, GLib.Variant.new_boolean(False))
|
|
Packit |
1470ea |
# action connected to the callback function
|
|
Packit |
1470ea |
awesome_action.connect("activate", self.awesome_callback)
|
|
Packit |
1470ea |
# action added to the application
|
|
Packit |
1470ea |
self.add_action(awesome_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a builder to add the UI designed with Glade to the grid:
|
|
Packit |
1470ea |
builder = Gtk.Builder()
|
|
Packit |
1470ea |
# get the file (if it is there)
|
|
Packit |
1470ea |
try:
|
|
Packit |
1470ea |
builder.add_from_file("menubar.ui")
|
|
Packit |
1470ea |
except:
|
|
Packit |
1470ea |
print("file not found")
|
|
Packit |
1470ea |
sys.exit()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# we use the method Gtk.Application.set_menubar(menubar) to add the menubar
|
|
Packit |
1470ea |
# and the menu to the application (Note: NOT the window!)
|
|
Packit |
1470ea |
self.set_menubar(builder.get_object("menubar"))
|
|
Packit |
1470ea |
self.set_app_menu(builder.get_object("appmenu"))
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for new
|
|
Packit |
1470ea |
def new_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("You clicked \"New\"")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for quit
|
|
Packit |
1470ea |
def quit_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("You clicked \"Quit\"")
|
|
Packit |
1470ea |
sys.exit()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for state
|
|
Packit |
1470ea |
def state_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("State is set to", parameter.get_string())
|
|
Packit |
1470ea |
action.set_state(parameter)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for awesome
|
|
Packit |
1470ea |
def awesome_callback(self, action, parameter):
|
|
Packit |
1470ea |
action.set_state(GLib.Variant.new_boolean(not action.get_state()))
|
|
Packit |
1470ea |
if action.get_state().get_boolean() is True:
|
|
Packit |
1470ea |
print("You checked \"Awesome\"")
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
print("You unchecked \"Awesome\"")
|
|
Packit |
1470ea |
|
|
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="mnemonics-and-accelerators"><title>Mnémoniques et raccourcis clavier</title>
|
|
Packit |
1470ea |
Les étiquettes peuvent contenir des mnémoniques. Les mnémoniques sont les caractères soulignés dans l'étiquette et sont utilisés pour se déplacer à l'aide des touches du clavier. Par exemple, « _Fichier » au lieu de seulement « Fichier » dans l'attribut barredemenu.ui de l'étiquette.
|
|
Packit |
1470ea |
Vous pouvez voir les mnémoniques en appuyant sur la touche <key>Alt</key>. Pour ouvrir le menu <gui>Fichier</gui>, appuyez sur la combinaison de touches <keyseq><key>Alt</key><key>F</key></keyseq>.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Des raccourcis clavier peuvent être explicitement ajoutés aux définitions de l'interface utilisateur. Il est par exemple usuel de pouvoir quitter une application en appuyant sur la combinaison <keyseq><key>Ctrl</key><key>Q</key></keyseq> ou enregistrer un fichier avec <keyseq><key>Ctrl</key><key>S</key></keyseq>. Pour ajouter un raccourci clavier à la définition de l'interface, ajoutez simplement un attribut « accel » à l'élément.
|
|
Packit |
1470ea |
<Primary>q</attribute>]]> will create the <keyseq><key>Ctrl</key><key>Q</key></keyseq> sequence when added to the Quit label item. Here, "Primary" refers to the <key>Ctrl</key> key on a PC or the <key>⌘</key> key on a Mac.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<item>
|
|
Packit |
1470ea |
<attribute name="label">_Quit</attribute>
|
|
Packit |
1470ea |
<attribute name="action">app.quit</attribute>
|
|
Packit |
1470ea |
<attribute name="accel"><Primary>q</attribute>
|
|
Packit |
1470ea |
</item>]]>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="translatable"><title>Chaînes de caractères traduisibles</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Since GNOME applications are being translated into <link href="http://l10n.gnome.org/languages/">many languages</link>, it is important that the strings in your application are translatable. To make a label translatable, simple set translatable="yes" :
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Quit</attribute>]]>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
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/gio/unstable/GSimpleAction.html">GSimpleAction</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkBuilder.html">GtkBuilder</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|