Las ventanas de desplazamiento se usan para crear un área de desplazamiento con otro control dentro de ella. Puede insertar cualquier tipo de control dentro de una ventana de desplazamiento, y será accesible da igual su tamaño usando barras de desplazamiento.
La siguiente función se usa para crear una nueva ventana de desplazamiento.
scrolled_window = gtk.ScrolledWindow(hadjustment=None, vadjustment=None) |
Donde el primer argumento es el ajuste para la dirección horizontal, y el segundo, el ajuste para la dirección vertical. Casi siempre se ponen a None o no se especifican.
scrolled_window.set_policy(hscrollbar_policy, vscrollbar_policy) |
Este método especifica la política a usar con respecto a las barras de desplazamiento. El primer argumento le fija la política a la barra de desplazamiento horizontal, y el segundo, la política de la barra de desplazamiento vertical.
La política puede tomar los valores POLICY_AUTOMATIC o POLICY_ALWAYS. POLICY_AUTOMATIC automáticamente decidirá si es necesario las barras de desplazamiento, mientras que POLICY_ALWAYS siempre dejará las barras de desplazamiento visibles.
Entonces puedes colocar tu objeto dentro de la ventana de desplazamiento usando el siguiente método.
scrolled_window.add_with_viewport(child)
|
El programa de ejemplo scrolledwin.py coloca una tabla con 100 botones biestado dentro de una ventana de desplazamiento. Sólo he comantado las partes que pueden ser nuevas para ti. La figura Figura 10.7. Ejemplo de Ventana de Desplazamiento muestra la ventana del programa:
El código fuente de scrolledwin.py es:
1 #!/usr/bin/env python
2
3 # example scrolledwin.py
4
5 import gtk
6
7 class ScrolledWindowExample:
8 def destroy(self, widget):
9 gtk.mainquit()
10
11 def __init__(self):
12 # Create a new dialog window for the scrolled window to be
13 # packed into.
14 window = gtk.Dialog()
15 window.connect("destroy", self.destroy)
16 window.set_title("ScrolledWindow example")
17 window.set_border_width(0)
18 window.set_size_request(300, 300)
19
20 # create a new scrolled window.
21 scrolled_window = gtk.ScrolledWindow()
22 scrolled_window.set_border_width(10)
23
24 # the policy is one of POLICY AUTOMATIC, or POLICY_ALWAYS.
25 # POLICY_AUTOMATIC will automatically decide whether you need
26 # scrollbars, whereas POLICY_ALWAYS will always leave the scrollbars
27 # there. The first one is the horizontal scrollbar, the second, the
28 # vertical.
29 scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)
30
31 # The dialog window is created with a vbox packed into it.
32 window.vbox.pack_start(scrolled_window, gtk.TRUE, gtk.TRUE, 0)
33 scrolled_window.show()
34
35 # create a table of 10 by 10 squares.
36 table = gtk.Table(10, 10, gtk.FALSE)
37
38 # set the spacing to 10 on x and 10 on y
39 table.set_row_spacings(10)
40 table.set_col_spacings(10)
41
42 # pack the table into the scrolled window
43 scrolled_window.add_with_viewport(table)
44 table.show()
45
46 # this simply creates a grid of toggle buttons on the table
47 # to demonstrate the scrolled window.
48 for i in range(10):
49 for j in range(10):
50 buffer = "button (%d,%d)" % (i, j)
51 button = gtk.ToggleButton(buffer)
52 table.attach(button, i, i+1, j, j+1)
53 button.show()
54
55 # Add a "close" button to the bottom of the dialog
56 button = gtk.Button("close")
57 button.connect_object("clicked", self.destroy, window)
58
59 # this makes it so the button is the default.
60 button.set_flags(gtk.CAN_DEFAULT)
61 window.action_area.pack_start( button, gtk.TRUE, gtk.TRUE, 0)
62
63 # This grabs this button to be the default button. Simply hitting
64 # the "Enter" key will cause this button to activate.
65 button.grab_default()
66 button.show()
67 window.show()
68
69 def main():
70 gtk.main()
71 return 0
72
73 if __name__ == "__main__":
74 ScrolledWindowExample()
75 main()
|
Prueba a cambiarle el tamaño a la ventana. Notarás cómo reaccionan las barras de desplazamiento. Puede que también quieras usar el método set_size_request() para fijar el tamaño por defecto de la ventana o de otros controles.