|
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="tooltip.py" xml:lang="fr">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Tooltip (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#misc"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="toolbar.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="toolbar_builder.py"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-08-20" 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>Ajouter des infobulles à vos éléments graphiques</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>Infobulle</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/tooltip.png"/>
|
|
Packit |
1470ea |
Une barre d'outil avec une infobulle (une image) pour un bouton.
|
|
Packit |
1470ea |
<note>This example builds on the <link xref="toolbar.py">Toolbar</link> example. </note>
|
|
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 |
from gi.repository import Gdk
|
|
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__(
|
|
Packit |
1470ea |
self, title="Toolbar with Tooltips Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(400, 200)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
toolbar = self.create_toolbar()
|
|
Packit |
1470ea |
toolbar.set_hexpand(True)
|
|
Packit |
1470ea |
toolbar.show()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
grid.attach(toolbar, 0, 0, 1, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
undo_action = Gio.SimpleAction.new("undo", None)
|
|
Packit |
1470ea |
undo_action.connect("activate", self.undo_callback)
|
|
Packit |
1470ea |
self.add_action(undo_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
fullscreen_action = Gio.SimpleAction.new("fullscreen", None)
|
|
Packit |
1470ea |
fullscreen_action.connect("activate", self.fullscreen_callback)
|
|
Packit |
1470ea |
self.add_action(fullscreen_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def create_toolbar(self):
|
|
Packit |
1470ea |
toolbar = Gtk.Toolbar()
|
|
Packit |
1470ea |
toolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# button for the "new" action
|
|
Packit |
1470ea |
new_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_NEW)
|
|
Packit |
1470ea |
# with a tooltip with a given text
|
|
Packit |
1470ea |
new_button.set_tooltip_text("Create a new file")
|
|
Packit |
1470ea |
new_button.set_is_important(True)
|
|
Packit |
1470ea |
toolbar.insert(new_button, 0)
|
|
Packit |
1470ea |
new_button.show()
|
|
Packit |
1470ea |
new_button.set_action_name("app.new")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# button for the "open" action
|
|
Packit |
1470ea |
open_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_OPEN)
|
|
Packit |
1470ea |
# with a tooltip with a given text in the Pango markup language
|
|
Packit |
1470ea |
open_button.set_tooltip_markup("Open an <i>existing</i> file")
|
|
Packit |
1470ea |
open_button.set_is_important(True)
|
|
Packit |
1470ea |
toolbar.insert(open_button, 1)
|
|
Packit |
1470ea |
open_button.show()
|
|
Packit |
1470ea |
open_button.set_action_name("app.open")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# button for the "undo" action
|
|
Packit |
1470ea |
undo_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_UNDO)
|
|
Packit |
1470ea |
# with a tooltip with an image
|
|
Packit |
1470ea |
# set True the property "has-tooltip"
|
|
Packit |
1470ea |
undo_button.set_property("has-tooltip", True)
|
|
Packit |
1470ea |
# connect to the callback function that for the tooltip
|
|
Packit |
1470ea |
# with the signal "query-tooltip"
|
|
Packit |
1470ea |
undo_button.connect("query-tooltip", self.undo_tooltip_callback)
|
|
Packit |
1470ea |
undo_button.set_is_important(True)
|
|
Packit |
1470ea |
toolbar.insert(undo_button, 2)
|
|
Packit |
1470ea |
undo_button.show()
|
|
Packit |
1470ea |
undo_button.set_action_name("win.undo")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# button for the "fullscreen/leave fullscreen" action
|
|
Packit |
1470ea |
self.fullscreen_button = Gtk.ToolButton.new_from_stock(
|
|
Packit |
1470ea |
Gtk.STOCK_FULLSCREEN)
|
|
Packit |
1470ea |
self.fullscreen_button.set_is_important(True)
|
|
Packit |
1470ea |
toolbar.insert(self.fullscreen_button, 3)
|
|
Packit |
1470ea |
self.fullscreen_button.set_action_name("win.fullscreen")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
return toolbar
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the callback function for the tooltip of the "undo" button
|
|
Packit |
1470ea |
def undo_tooltip_callback(self, widget, x, y, keyboard_mode, tooltip):
|
|
Packit |
1470ea |
# set the text for the tooltip
|
|
Packit |
1470ea |
tooltip.set_text("Undo your last action")
|
|
Packit |
1470ea |
# set an icon fot the tooltip
|
|
Packit |
1470ea |
tooltip.set_icon_from_stock("gtk-undo", Gtk.IconSize.MENU)
|
|
Packit |
1470ea |
# show the tooltip
|
|
Packit |
1470ea |
return True
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def undo_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("You clicked \"Undo\".")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def fullscreen_callback(self, action, parameter):
|
|
Packit |
1470ea |
is_fullscreen = self.get_window().get_state(
|
|
Packit |
1470ea |
) & Gdk.WindowState.FULLSCREEN != 0
|
|
Packit |
1470ea |
if not is_fullscreen:
|
|
Packit |
1470ea |
self.fullscreen_button.set_stock_id(Gtk.STOCK_LEAVE_FULLSCREEN)
|
|
Packit |
1470ea |
self.fullscreen()
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
self.fullscreen_button.set_stock_id(Gtk.STOCK_FULLSCREEN)
|
|
Packit |
1470ea |
self.unfullscreen()
|
|
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 |
new_action = Gio.SimpleAction.new("new", None)
|
|
Packit |
1470ea |
new_action.connect("activate", self.new_callback)
|
|
Packit |
1470ea |
app.add_action(new_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
open_action = Gio.SimpleAction.new("open", None)
|
|
Packit |
1470ea |
open_action.connect("activate", self.open_callback)
|
|
Packit |
1470ea |
app.add_action(open_action)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def new_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("You clicked \"New\".")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def open_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("You clicked \"Open\".")
|
|
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éthodes utiles pour un élément graphique infobulle (Tooltip)</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Les fonctions set_tooltip_text(text) et set_tooltip_markup(text) peuvent être utilisées pour ajouter une infobulle textuelle (ou un texte dans le langage de balisage Pango) à un élément graphique.
|
|
Packit |
1470ea |
Pour des infobulles plus complexes, par exemple pour une infobulle sous la forme d'image :
|
|
Packit |
1470ea |
<steps>
|
|
Packit |
1470ea |
<item>Attribuez la valeur True (vrai) à la propriété "has-tooltip" de l'élément graphique ; de cette façon GTK+ surveille les événements liés aux mouvements de la souris et tous ceux qui sont nécessaires afin de déterminer quand et où afficher une infobulle. </item>
|
|
Packit |
1470ea |
<item>Connectez le signal "query-tooltip" . Ce signal est émis lorsqu'une infobulle doit s'afficher. Un des arguments transmis au gestionnaire de signaux est un objet GtkTooltip. C'est l'objet que nous allons afficher comme infobulle et qui peut être manipulé dans votre fonction de rappel (callback) en utilisant des fonctions comme set_icon() . Ce sont des fonctions servant à paramétrer le balisage de l'infobulle (set_markup(text) ), à définir une image à partir de la collection d'icônes (set_icon_from_stock(stock_id, size) ), ou même à positionner un élément graphique personnalisé (set_custom(widget) ). </item>
|
|
Packit |
1470ea |
<item>Renvoyez la valeur True à partir de votre gestionnaire query-tooltip. Cela provoque l'affichage de l'infobulle. Si vous renvoyez False , elle n'est pas affichée. </item>
|
|
Packit |
1470ea |
</steps>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Dans le cas peut probable où vous voulez avoir plus de contrôle sur l'infobulle qui est sur le point d'être affichée, vous pouvez définir votre propre GtkWindow qui sera utilisée comme fenêtre d'infobulle. Cela fonctionne comme ceci :
|
|
Packit |
1470ea |
<steps>
|
|
Packit |
1470ea |
<item>Paramétrez "has-tooltip" et connectez le à "query-tooltip" comme auparavant. </item>
|
|
Packit |
1470ea |
<item>Utilisez set_tooltip_window() sur l'élément graphique pour définir un GtkWindow créé par vos soins comme fenêtre d'infobulle. </item>
|
|
Packit |
1470ea |
<item>Dans la fonction de rappel "query-tooltip" vous pouvez accéder à votre fenêtre en utilisant get_tooltip_window() et la manipuler comme bon vous semble. La sémantiques pour la valeur retournée est exactement comme ci-dessus, renvoyez True pour afficher la fenêtre, False dans le cas contraire. </item>
|
|
Packit |
1470ea |
</steps>
|
|
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/stable/GtkTooltip.html">GtkTooltip</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkToolbar.html">GtkToolbar</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkWidget.html">GtkWidget</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/gtk3-Stock-Items.html">Stock Items</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|