El contenedor Layout (Disposición) es similar al contenedor Fixed (Fijo) excepto en que implementa un área de desplazamiento infinita (donde infinito es menor que 2^32). El sistema de ventanas X tiene una limitación que hace uqe las ventanas sólo puedan tener 32767 píxeles de ancho o alto. El contenedor Layout soluciona esta limitación haciendo algunos trucos exóticos mediante el uso de gravedad de ventana y de bit, para que tengas un desplazamiento suave incluso cuando tienes muchos controles en tu área de desplazamiento.
Un contenedor Layout se crea con:
layout = gtk.Layout(hadjustment=None, vadjustment=None) |
Como puedes ver, puedes especificar opcionalmente los objetos Adjustment que el control Layout usará para su desplazamiento. Si no especificas los objetos Adjustment , unos nuevos se crearán.
Puedes añadir y mover controles en el contenedor Layout usando los dos siguientes métodos:
layout.put(child_widget, x, y) layout.move(child_widget, x, y) |
El tamaño del contenedor Layout se puede establecer y consultar usando los siguientes métodos:
layout.set_size(width, height) size = layout.get_size() |
Los últimos cuatro métodos para los controles Layout widgets son para manipular los controles de ajuste horizontal y vertical:
hadj = layout.get_hadjustment() vadj = layout.get_vadjustment() layout.set_hadjustment(adjustment) layout.set_vadjustment(adjustment) |
El programa de ejemplo layout.py crea tres botones y los pone en un control disposición. Cuando se hace clic en un botón, se mueve a una posición aleatoria en el control disposición. La figura Figura 10.3. Ejemplo de Disposición ilustra la ventana inicial del programa:
El código fuente de layout.py es:
1 #!/usr/bin/env python
2
3 # example layout.py
4
5 import gtk
6 import whrandom
7
8 class LayoutExample:
9 def WindowDeleteEvent(self, widget, event):
10 # return false so that window will be destroyed
11 return gtk.FALSE
12
13 def WindowDestroy(self, widget, *data):
14 # exit main loop
15 gtk.mainquit()
16
17 def ButtonClicked(self, button):
18 # move the button
19 self.layout.move(button, whrandom.randint(0,500),
20 whrandom.randint(0,500))
21
22 def __init__(self):
23 # create the top level window
24 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
25 window.set_title("Layout Example")
26 window.set_default_size(300, 300)
27 window.connect("delete-event", self.WindowDeleteEvent)
28 window.connect("destroy", self.WindowDestroy)
29 # create the table and pack into the window
30 table = gtk.Table(2, 2, gtk.FALSE)
31 window.add(table)
32 # create the layout widget and pack into the table
33 self.layout = gtk.Layout(None, None)
34 self.layout.set_size(600, 600)
35 table.attach(self.layout, 0, 1, 0, 1, gtk.FILL|gtk.EXPAND,
36 gtk.FILL|gtk.EXPAND, 0, 0)
37 # create the scrollbars and pack into the table
38 vScrollbar = gtk.VScrollbar(None)
39 table.attach(vScrollbar, 1, 2, 0, 1, gtk.FILL|gtk.SHRINK,
40 gtk.FILL|gtk.SHRINK, 0, 0)
41 hScrollbar = gtk.HScrollbar(None)
42 table.attach(hScrollbar, 0, 1, 1, 2, gtk.FILL|gtk.SHRINK,
43 gtk.FILL|gtk.SHRINK, 0, 0)
44 # tell the scrollbars to use the layout widget's adjustments
45 vAdjust = self.layout.get_vadjustment()
46 vScrollbar.set_adjustment(vAdjust)
47 hAdjust = self.layout.get_hadjustment()
48 hScrollbar.set_adjustment(hAdjust)
49 # create 3 buttons and put them into the layout widget
50 button = gtk.Button("Press Me")
51 button.connect("clicked", self.ButtonClicked)
52 self.layout.put(button, 0, 0)
53 button = gtk.Button("Press Me")
54 button.connect("clicked", self.ButtonClicked)
55 self.layout.put(button, 100, 0)
56 button = gtk.Button("Press Me")
57 button.connect("clicked", self.ButtonClicked)
58 self.layout.put(button, 200, 0)
59 # show all the widgets
60 window.show_all()
61
62 def main():
63 # enter the main loop
64 gtk.main()
65 return 0
66
67 if __name__ == "__main__":
68 LayoutExample()
69 main()
|