|
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="toolbar.py" xml:lang="es">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Toolbar (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#menu-combo-toolbar"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="grid.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="tooltip.py"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-06-05" 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>Una barra de botones y otros widgets</desc>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
|
|
Packit |
1470ea |
<mal:name>Daniel Mustieles</mal:name>
|
|
Packit |
1470ea |
<mal:email>daniel.mustieles@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011 - 2017</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>Nicolás Satragno</mal:name>
|
|
Packit |
1470ea |
<mal:email>nsatragno@gmail.com</mal:email>
|
|
Packit |
1470ea |
<mal:years>2012 - 2013</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>Jorge González</mal:name>
|
|
Packit |
1470ea |
<mal:email>jorgegonz@svn.gnome.org</mal:email>
|
|
Packit |
1470ea |
<mal:years>2011</mal:years>
|
|
Packit |
1470ea |
</mal:credit>
|
|
Packit |
1470ea |
</info>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<title>Barra de herramientas</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/toolbar.png"/>
|
|
Packit |
1470ea |
Un ejemplo de barra de herramientas con botones (con iconos del almacén).
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Código usado para generar este ejemplo</title>
|
|
Packit |
1470ea |
|
|
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__(self, title="Toolbar Example", application=app)
|
|
Packit |
1470ea |
self.set_default_size(400, 200)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a grid to attach the toolbar
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a toolbar created in the method create_toolbar (see below)
|
|
Packit |
1470ea |
toolbar = self.create_toolbar()
|
|
Packit |
1470ea |
# with extra horizontal space
|
|
Packit |
1470ea |
toolbar.set_hexpand(True)
|
|
Packit |
1470ea |
# show the toolbar
|
|
Packit |
1470ea |
toolbar.show()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# attach the toolbar to the grid
|
|
Packit |
1470ea |
grid.attach(toolbar, 0, 0, 1, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# add the grid to the window
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create the actions that control the window and connect their signal to a
|
|
Packit |
1470ea |
# callback method (see below):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# undo
|
|
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
|
|
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 |
# a method to create the toolbar
|
|
Packit |
1470ea |
def create_toolbar(self):
|
|
Packit |
1470ea |
# a toolbar
|
|
Packit |
1470ea |
toolbar = Gtk.Toolbar()
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# which is the primary toolbar of the application
|
|
Packit |
1470ea |
toolbar.get_style_context().add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# create a button for the "new" action, with a stock image
|
|
Packit |
1470ea |
new_button = Gtk.ToolButton.new_from_stock(Gtk.STOCK_NEW)
|
|
Packit |
1470ea |
# label is shown
|
|
Packit |
1470ea |
new_button.set_is_important(True)
|
|
Packit |
1470ea |
# insert the button at position in the toolbar
|
|
Packit |
1470ea |
toolbar.insert(new_button, 0)
|
|
Packit |
1470ea |
# show the button
|
|
Packit |
1470ea |
new_button.show()
|
|
Packit |
1470ea |
# set the name of the action associated with the button.
|
|
Packit |
1470ea |
# The action controls the application (app)
|
|
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 |
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 |
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 the complete toolbar
|
|
Packit |
1470ea |
return toolbar
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback method for undo
|
|
Packit |
1470ea |
def undo_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("You clicked \"Undo\".")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback method for fullscreen / leave fullscreen
|
|
Packit |
1470ea |
def fullscreen_callback(self, action, parameter):
|
|
Packit |
1470ea |
# check if the state is the same as Gdk.WindowState.FULLSCREEN, which
|
|
Packit |
1470ea |
# is a bit flag
|
|
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 |
# create the actions that control the window and connect their signal to a
|
|
Packit |
1470ea |
# callback method (see below):
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# new
|
|
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
|
|
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 |
# callback method for new
|
|
Packit |
1470ea |
def new_callback(self, action, parameter):
|
|
Packit |
1470ea |
print("You clicked \"New\".")
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback method for open
|
|
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étodos útiles para un widget de barra de herramientas</title>
|
|
Packit |
1470ea |
En la línea 32, la señal «activate» de la acción undo_action se conecta a la función de retorno de llamada undo_callback() usando action.connect(señal, función de retorno de llamada) . Consulte la <link xref="signals-callbacks.py"/> para una explicación más detallada.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>Use insert(tool_item, position) para insertar el tool_item en position . Si position es negativo, el elemento se añade al final de la barra de herramientas. </item>
|
|
Packit |
1470ea |
<item>get_item_index(tool_item) obtiene la posición de tool_item en la barra de herramientas. </item>
|
|
Packit |
1470ea |
<item>get_n_items() devuelve el número de elementos en la barra de herramientas; get_nth_item(position) devuelve el elemento en la posición position . </item>
|
|
Packit |
1470ea |
<item>Si la barra de herramientas no tiene espacio para todos los elementos del menú, y set_show_arrow(True) , los elementos que no entran se muestran a través de un menú flotante. </item>
|
|
Packit |
1470ea |
<item>set_icon_size(icon_size) establece el tamaño de los iconos en la barra de herramientas; icon_size puede ser uno de Gtk.IconSize.INVALID, Gtk.IconSize.MENU, Gtk.IconSize.SMALL_TOOLBAR, Gtk.IconSize.LARGE_TOOLBAR, Gtk.IconSize.BUTTON, Gtk.IconSize.DND, Gtk.IconSize.DIALOG . Esto solo debe usarse para barras de herramientas con propósitos especiales, las barras de herramientas de aplicaciones normales deben respetar las preferencias del usuario para el tamaño de los iconos. unset_icon_size() restablece las preferencias establecidas con set_icon_size(icon_size) , para que las preferencias de usuario se usen para determinar el tamaño de los iconos. </item>
|
|
Packit |
1470ea |
<item>set_style(style) , donde style es uno de Gtk.ToolbarStyle.ICONS, Gtk.ToolbarStyle.TEXT, Gtk.ToolbarStyle.BOTH, Gtk.ToolbarStyle.BOTH_HORIZ , determina si la barra de herramientas muestra solo iconos, solo texto, o ambos (apilados verticalmente o uno al lado de otro). Para dejar que las preferencias del usuario determinen el estilo de la barra de herramientas, y restablecer el estilo de barra de herramientas, use unset_style() . </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="reference">
|
|
Packit |
1470ea |
<title>Referencias de la API</title>
|
|
Packit |
1470ea |
En este ejemplo se usa lo siguiente:
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkToolbar.html">GtkToolbar</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkToolButton.html">GtkToolButton</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkToolItem.html">GtkToolItem</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/gtk3-Stock-Items.html">Elementos del almacén</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkActionable.html">GtkActionable</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/unstable/GtkWidget.html">GtkWidget</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gdk3/unstable/gdk3-Event-Structures.html#GdkEventWindowState">Estructuras de eventos</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|