10.11. Barra de Herramientas

Las Toolbars (Barras de Herramientas) se usan normalmente para agrupar un número de controles para simplificar la personalización de su apariencia y disposición. Típicamente una barra de herramientas consiste en botones con iconos, etiquetas y pistas, pero cualquier otro control se puede poner en una barra de herramientas. Finalmente, los elementos se pueden colocar horizontal o verticalmente y se pueden ver los botones con iconos, texto, o ambos.

Crear una barra de herramientas se consigue (como uno podría sospechar) con la siguiente función:

  toolbar = gtk.Toolbar()

Después de crear una barra de herramientas uno puede añadir al principio, al final o en otro posición, items (textos simples) o elementos (cualquier tipo de control) en la barra de herramientas. Para describir un item necesitamos un texto, un texto para la pista, un texto privado para la pista, un icono para el botón y una retrollamada. Por ejemplo, para añadir al principio o al final un item puedes usar los siguientes métodos:

  toolbar.append_item(text, tooltip_text, tooltip_private_text, icon, callback, user_data=None)

  toolbar.prepend_item(text, tooltip_text, tooltip_private_text, icon, callback, user_data)

Si quieres usar el método insert_item(), el único parámetro adicional que debe especificarse, es la posición en la cual el item debe insertarse, así:

  toolbar.insert_item(text, tooltip_text, tooltip_private_text, icon, callback, 
                      user_data, position)

Para añadir espacios entre items de la barra de herramientas de forma sencilla, puedes usar los siguientes métodos:

  toolbar.append_space()

  toolbar.prepend_space()

  toolbar.insert_space(position)

Si es necesario, la orientación de una barra de herramientas, su estilo y el hecho de que las pistas estén disponibles, se puede cambiar sobre la marcha usando los siguientes métodos:

  toolbar.set_orientation(orientation)

  toolbar.set_style(style)

  toolbar.set_tooltips(enable)

Donde la orientation puede ser ORIENTATION_HORIZONTAL o ORIENTATION_VERTICAL. El style se usa para especificar la apariencia de la barra de herramientas y puede ser TOOLBAR_ICONS (iconos), TOOLBAR_TEXT (texto), o TOOLBAR_BOTH (ambos). El argumento enable puede ser o TRUE o FALSE.

Para mostrar algunas otras cas que se pueden hacer con una barra de herramientas veamos el siguiente programa de ejemplo toolbar.py (interrumpiremos el listado con algunas explicaciones adicionales):

    1   #!/usr/bin/env python
    2   
    3   # example toolbar.py
    4   
    5   import gtk
    6	
    7	class ToolbarExample:
    8	    # This method is connected to the Close button or
    9	    # closing the window from the WM
   10	    def delete_event(self, widget, event=None):
   11	        gtk.mainquit()
   12	        return gtk.FALSE
   13	

Este principio debería resultarte familiar si no es tu primer programa PyGTK. No obstante hay una cosa más, vamos a importar un bonito dibujo XPM (gtk.xpm) para que nos sirva de icono para todos los botones. La linea 8 empieza la clase de ejemplo ToolbarExample y las lineas 10-12 definen el método de retrollamada que finalizará el programa.

   14	    # that's easy... when one of the buttons is toggled, we just
   15	    # check which one is active and set the style of the toolbar
   16	    # accordingly
   17	    def radio_event(self, widget, toolbar):
   18	        if self.text_button.get_active(): 
   19	            toolbar.set_style(gtk.TOOLBAR_TEXT)
   20	        elif self.icon_button.get_active():
   21	            toolbar.set_style(gtk.TOOLBAR_ICONS)
   22	        elif self.both_button.get_active():
   23	            toolbar.set_style(gtk.TOOLBAR_BOTH)
   24	
   25	    # even easier, just check given toggle button and enable/disable 
   26	    # tooltips
   27	    def toggle_event(self, widget, toolbar):
   28	        toolbar.set_tooltips(widget.get_active())
   29	

Las lineas 17-28 son dos métodos de retrollamada que se llamarán cuando uno de los botones de la barra de herramientas se active. Debería conocer cosas como esta si ya has usado botones biestado (y botones de exclusión mútua).

   30	    def __init__(self):
   31	        # Here is our main window (a dialog) and a handle for the handlebox
   32	        # Ok, we need a toolbar, an icon with a mask (one for all of 
   33	        # the buttons) and an icon widget to put this icon in (but 
   34	        # we'll create a separate widget for each button)
   35	        # create a new window with a given title, and nice size
   36	        dialog = gtk.Dialog()
   37	        dialog.set_title("GTKToolbar Tutorial")
   38	        dialog.set_size_request(450, 250)
   39	        dialog.set_resizable(gtk.TRUE)
   40	
   41	        # typically we quit if someone tries to close us
   42	        dialog.connect("delete_event", self.delete_event)
   43	
   44	        # to make it nice we'll put the toolbar into the handle box, 
   45	        # so that it can be detached from the main window
   46	        handlebox = gtk.HandleBox()
   47	        dialog.vbox.pack_start(handlebox, gtk.FALSE, gtk.FALSE, 5)
   48	

Lo anterior debería ser común a cualquier otra aplicación PyGTK. Inicialización de una instancia de ToolbarExample, creación de la ventana, etc. Sólo hay una cosa que probablemente necesite más explicación: una caja con mango (HandleBox). Una caja con mango es simplemente otra caja cualquiera que puede usarse para meter controles dentro. La diferencia entre ella y las cajas típicas es que puede despegarse de la ventana padre (o, de hecho, la caja con mango permanece en el padre, pero se reduce a un rectángulo muy pequeño, mientras que todos sus contenidos cambian de padre a una nueva ventana flotante). Normalmente es bueno tener una barra de herramientas desplegable, por lo que estos dos controles suelen ir juntos normalmente.

   49	        # toolbar will be horizontal, with both icons and text, and
   50	        # with 5pxl spaces between items and finally, 
   51	        # we'll also put it into our handlebox
   52	        toolbar = gtk.Toolbar()
   53	        toolbar.set_orientation(gtk.ORIENTATION_HORIZONTAL)
   54	        toolbar.set_style(gtk.TOOLBAR_BOTH)
   55	        toolbar.set_border_width(5)
   56	        handlebox.add(toolbar)
   57	

Bien, lo que hacemos arriba es una simple inicialización del control de la barra de herramientas.

   58	        # our first item is (close) button
   59	        iconw = gtk.Image() # icon widget
   60	        iconw.set_from_file("gtk.xpm")
   61	        close_button = toolbar.append_item(
   62	            "Close",           # button label
   63	            "Closes this app", # this button's tooltip
   64	            "Private",         # tooltip private info
   65	            iconw,             # icon widget
   66	            self.delete_event) # a signal
   67	        toolbar.append_space() # space after item

En el código anterior puedes ver el caso más simple: añadir un botón a la barra de herramientas. Justo antes de añadir el nuevo item, tenemos que construir un control de imágen que servirá como icono para este item; este paso tiene que repetirse para cada item nuevo. Justo después del item añadimos también espacio, para que los items siguientes no se tocan unos a otros. Como puedes ver el método append_item() devuelve una referencia a nuestro control de botón recién creado, para que podamos trabajar con él de la manera normal.

   69	        # now, let's make our radio buttons group...
   70	        iconw = gtk.Image() # icon widget
   71	        iconw.set_from_file("gtk.xpm")
   72	        icon_button = toolbar.append_element(
   73	            gtk.TOOLBAR_CHILD_RADIOBUTTON, # type of element
   74	            None,                          # widget
   75	            "Icon",                        # label
   76	            "Only icons in toolbar",       # tooltip
   77	            "Private",                     # tooltip private string
   78	            iconw,                         # icon
   79	            self.radio_event,              # signal
   80	            toolbar)                       # data for signal
   81	        toolbar.append_space()
   82	        self.icon_button = icon_button

Aqui empezamos a crear un grupo de botones de exclusión mútua. Para hacer esto usamos el método append_element() . De hecho, usando este método, uno puede también puede añadir items normales o incluso espacios (type = gtk.TOOLBAR_CHILD_SPACE (espacio) o gtk.TOOLBAR_CHILD_BUTTON (botón)). En el ejemplo anterior hemos empezado a crear un grupo de botones de exclusión mútua. Cuando se crean otros botones de exclusión mútua se necesita una referencia al botón anterior en el grupo, para que se pueda construir una lista de botones fácilmente (mira la sección RadioButtons de este tutorial). También tenemos una referencia al botón en la instancia de ToolbarExample para acceder a él más tare.

   84	        # following radio buttons refer to previous ones
   85	        iconw = gtk.Image() # icon widget
   86	        iconw.set_from_file("gtk.xpm")
   87	        text_button = toolbar.append_element(
   88	            gtk.TOOLBAR_CHILD_RADIOBUTTON,
   89	            icon_button,
   90	            "Text",
   91	            "Only texts in toolbar",
   92	            "Private",
   93	            iconw,
   94	            self.radio_event,
   95	            toolbar)
   96	        toolbar.append_space()
   97	        self.text_button = text_button
   98	
   99	        iconw = gtk.Image() # icon widget
  100	        iconw.set_from_file("gtk.xpm")
  101	        both_button = toolbar.append_element(
  102	            gtk.TOOLBAR_CHILD_RADIOBUTTON,
  103	            text_button,
  104	            "Both",
  105	            "Icons and text in toolbar",
  106	            "Private",
  107	            iconw,
  108	            self.radio_event,
  109	            toolbar)
  110	        toolbar.append_space()
  111	        self.both_button = both_button
  112	        both_button.set_active(gtk.TRUE)

Creamos otros botones de exclusión mútua de la misma forma excepto que le pasamos uno de los botones creados al método append_element() para especificar el grupo.

Al final tenemos que establecer el estado de uno de los botones manualmente (si no todos tienen el estado activo, impediendonos que los vayamos intercambiando).

  114	        # here we have just a simple toggle button
  115	        iconw = gtk.Image() # icon widget
  116	        iconw.set_from_file("gtk.xpm")
  117	        tooltips_button = toolbar.append_element(
  118	            gtk.TOOLBAR_CHILD_TOGGLEBUTTON,
  119	            None,
  120	            "Tooltips",
  121	            "Toolbar with or without tips",
  122	            "Private",
  123	            iconw,
  124	            self.toggle_event,
  125	            toolbar)
  126	        toolbar.append_space()
  127	        tooltips_button.set_active(gtk.TRUE)

Un botón biestado se puede crear de la forma obvia (si ya has creado botones de exclusión mútua).

  129	        # to pack a widget into toolbar, we only have to 
  130	        # create it and append it with an appropriate tooltip
  131	        entry = gtk.Entry()
  132	        toolbar.append_widget(entry,  "This is just an entry", "Private")
  133	
  134	        # well, it isn't created within the toolbar, so we must still show it
  135	        entry.show()

Como verás, añadir cualquier tipo de control a una barra de herramientas es fácil. La única cosa que tienes que recordar es que este control debe mostrarse manualmente (al contrario que los items, que se mostrarán de golpe con la barra de herramientas).

  137	        # that's it ! let's show everything.
  138	        toolbar.show()
  139	        handlebox.show()
  140	        dialog.show()
  141	
  142	def main():
  143	    # rest in gtk_main and wait for the fun to begin!
  144	    gtk.main()
  145	    return 0
  146	
  147	if __name__ == "__main__":
  148	    ToolbarExample()
  149	    main()

La linea 140 termina la definición de la clase ToolbarExample. Las lineas 142-145 definen la función main() que simplemente llama a la función gtk.main() para empezar el bucle de procesamiento de eventos. Las lineas 147-149 crean una instancia de ToolbarExample y luego entran en el bucle de procesamiento de eventos. Asi termina el tutorial de la barra de herramientas. Por supuesto, para apreciarla en su máximo esplendor necesitas también el icono XPM, gtk.xpm. La figura Figura 10.8. Ejemplo de Barra de Herramientas muestar la ventana resultante:

Figura 10.8. Ejemplo de Barra de Herramientas

Ejemplo de Barra de Herramientas