Las ButtonBoxes (Cajas de Botones) proporcionan un sistema fácil de agrupar rápidamente botones. Vienen en variedades horizontales y verticales. Puedes crear una nueva ButtonBox con una de las siguiente llamadas, que crean una caja horizontal o vertical, respectivamente:
hbutton_box = gtk.HButtonBox() vbutton_box = gtk.VButtonBox() |
Los únicos métodos de las cajas de botones afectan a la disposición de los botones.
La disposición de los botones dentro de la caja se establece usando:
button_box.set_layout(layout_style)
|
El argumento layout_style puede tomar uno de los siguientes valores:
BUTTONBOX_DEFAULT_STYLE # estilo predeterminado BUTTONBOX_SPREAD # esparcidos BUTTONBOX_EDGE # filo BUTTONBOX_START # principio BUTTONBOX_END # fin |
El valor actual de layout_style se puede consultar usando:
layout_style = button_box.get_layout() |
Los botones se añaden a la ButtonBox usando el típico método del Container (Contenedor):
button_box.add(widget)
|
El programa de ejemplo buttonbox.py ilustra todos los tipos de disposición de las ButtonBoxes. La ventana resultante es:
El código fuente del programa buttonbox.py es:
1 #!/usr/bin/env python
2
3 # example buttonbox.py
4
5 import gtk
6
7 class ButtonBoxExample:
8 # Create a Button Box with the specified parameters
9 def create_bbox(self, horizontal, title, spacing, layout):
10 frame = gtk.Frame(title)
11
12 if horizontal:
13 bbox = gtk.HButtonBox()
14 else:
15 bbox = gtk.VButtonBox()
16
17 bbox.set_border_width(5)
18 frame.add(bbox)
19
20 # Set the appearance of the Button Box
21 bbox.set_layout(layout)
22 bbox.set_spacing(spacing)
23
24 button = gtk.Button(stock=gtk.STOCK_OK)
25 bbox.add(button)
26
27 button = gtk.Button(stock=gtk.STOCK_CANCEL)
28 bbox.add(button)
29
30 button = gtk.Button(stock=gtk.STOCK_HELP)
31 bbox.add(button)
32
33 return frame
34
35 def __init__(self):
36 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
37 window.set_title("Button Boxes")
38
39 window.connect("destroy", gtk.mainquit)
40
41 window.set_border_width(10)
42
43 main_vbox = gtk.VBox(gtk.FALSE, 0)
44 window.add(main_vbox)
45
46 frame_horz = gtk.Frame("Horizontal Button Boxes")
47 main_vbox.pack_start(frame_horz, gtk.TRUE, gtk.TRUE, 10)
48
49 vbox = gtk.VBox(gtk.FALSE, 0)
50 vbox.set_border_width(10)
51 frame_horz.add(vbox)
52
53 vbox.pack_start(self.create_bbox(gtk.TRUE, "Spread (spacing 40)",
54 40, gtk.BUTTONBOX_SPREAD),
55 gtk.TRUE, gtk.TRUE, 0)
56
57 vbox.pack_start(self.create_bbox(gtk.TRUE, "Edge (spacing 30)",
58 30, gtk.BUTTONBOX_EDGE),
59 gtk.TRUE, gtk.TRUE, 5)
60
61 vbox.pack_start(self.create_bbox(gtk.TRUE, "Start (spacing 20)",
62 20, gtk.BUTTONBOX_START),
63 gtk.TRUE, gtk.TRUE, 5)
64
65 vbox.pack_start(self.create_bbox(gtk.TRUE, "End (spacing 10)",
66 10, gtk.BUTTONBOX_END),
67 gtk.TRUE, gtk.TRUE, 5)
68
69 frame_vert = gtk.Frame("Vertical Button Boxes")
70 main_vbox.pack_start(frame_vert, gtk.TRUE, gtk.TRUE, 10)
71
72 hbox = gtk.HBox(gtk.FALSE, 0)
73 hbox.set_border_width(10)
74 frame_vert.add(hbox)
75
76 hbox.pack_start(self.create_bbox(gtk.FALSE, "Spread (spacing 5)",
77 5, gtk.BUTTONBOX_SPREAD),
78 gtk.TRUE, gtk.TRUE, 0)
79
80 hbox.pack_start(self.create_bbox(gtk.FALSE, "Edge (spacing 30)",
81 30, gtk.BUTTONBOX_EDGE),
82 gtk.TRUE, gtk.TRUE, 5)
83
84 hbox.pack_start(self.create_bbox(gtk.FALSE, "Start (spacing 20)",
85 20, gtk.BUTTONBOX_START),
86 gtk.TRUE, gtk.TRUE, 5)
87
88 hbox.pack_start(self.create_bbox(gtk.FALSE, "End (spacing 20)",
89 20, gtk.BUTTONBOX_END),
90 gtk.TRUE, gtk.TRUE, 5)
91
92 window.show_all()
93
94 def main():
95 # Enter the event loop
96 gtk.main()
97 return 0
98
99 if __name__ == "__main__":
100 ButtonBoxExample()
101 main()
|