|
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="buttonbox.py" xml:lang="es">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Botonera (Python)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.py#layout"/>
|
|
Packit |
1470ea |
<link type="seealso" xref="button.py"/>
|
|
Packit |
1470ea |
<link type="next" xref="statusbar.py"/>
|
|
Packit |
1470ea |
<revision version="0.2" date="2012-08-01" status="stub"/>
|
|
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>Un contenedor para organizar botones</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>Botonera</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/buttonbox_calculator.png"/>
|
|
Packit |
1470ea |
Una calculadora: los botones se agrupan en botoneras horizontales.
|
|
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 |
from gi.repository import Gtk
|
|
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="Calculator", application=app)
|
|
Packit |
1470ea |
self.set_default_size(350, 200)
|
|
Packit |
1470ea |
self.set_border_width(10)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# an entry
|
|
Packit |
1470ea |
self.entry = Gtk.Entry()
|
|
Packit |
1470ea |
# with an initial text
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
# text aligned on the right
|
|
Packit |
1470ea |
self.entry.set_alignment(1)
|
|
Packit |
1470ea |
# the text in the entry cannot be modified writing in it
|
|
Packit |
1470ea |
self.entry.set_can_focus(False)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# a grid
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.set_row_spacing(5)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# to attach the entry
|
|
Packit |
1470ea |
grid.attach(self.entry, 0, 0, 1, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# the labels for the buttons
|
|
Packit |
1470ea |
buttons = [7, 8, 9, '/',
|
|
Packit |
1470ea |
4, 5, 6, '*',
|
|
Packit |
1470ea |
1, 2, 3, '-',
|
|
Packit |
1470ea |
'C', 0, '=', '+']
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# each row is a ButtonBox, attached to the grid
|
|
Packit |
1470ea |
for i in range(4):
|
|
Packit |
1470ea |
hbox = Gtk.ButtonBox.new(Gtk.Orientation.HORIZONTAL)
|
|
Packit |
1470ea |
hbox.set_spacing(5)
|
|
Packit |
1470ea |
grid.attach(hbox, 0, i + 1, 1, 1)
|
|
Packit |
1470ea |
# each ButtonBox has 4 buttons, connected to the callback function
|
|
Packit |
1470ea |
for j in range(4):
|
|
Packit |
1470ea |
button = Gtk.Button(label=buttons[i * 4 + j])
|
|
Packit |
1470ea |
button.set_can_focus(False)
|
|
Packit |
1470ea |
button.connect("clicked", self.button_clicked)
|
|
Packit |
1470ea |
hbox.add(button)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# some variables for the calculations
|
|
Packit |
1470ea |
self.first_number = 0
|
|
Packit |
1470ea |
self.second_number = 0
|
|
Packit |
1470ea |
self.counter = 0
|
|
Packit |
1470ea |
self.operation = ""
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# add the grid to the window
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# callback function for all the buttons
|
|
Packit |
1470ea |
def button_clicked(self, button):
|
|
Packit |
1470ea |
# for the operations
|
|
Packit |
1470ea |
if button.get_label() == '+':
|
|
Packit |
1470ea |
self.counter += 1
|
|
Packit |
1470ea |
if self.counter > 1:
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
self.operation = "plus"
|
|
Packit |
1470ea |
elif button.get_label() == '-':
|
|
Packit |
1470ea |
self.counter += 1
|
|
Packit |
1470ea |
if self.counter > 1:
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
self.operation = "minus"
|
|
Packit |
1470ea |
elif button.get_label() == '*':
|
|
Packit |
1470ea |
self.counter += 1
|
|
Packit |
1470ea |
if self.counter > 1:
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
self.operation = "multiplication"
|
|
Packit |
1470ea |
elif button.get_label() == '/':
|
|
Packit |
1470ea |
self.counter += 1
|
|
Packit |
1470ea |
if self.counter > 1:
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
self.operation = "division"
|
|
Packit |
1470ea |
# for =
|
|
Packit |
1470ea |
elif button.get_label() == '=':
|
|
Packit |
1470ea |
self.do_operation()
|
|
Packit |
1470ea |
self.entry.set_text(str(self.first_number))
|
|
Packit |
1470ea |
self.counter = 1
|
|
Packit |
1470ea |
# for Cancel
|
|
Packit |
1470ea |
elif button.get_label() == 'C':
|
|
Packit |
1470ea |
self.first_number = 0
|
|
Packit |
1470ea |
self.second_number = 0
|
|
Packit |
1470ea |
self.counter = 0
|
|
Packit |
1470ea |
self.entry.set_text('')
|
|
Packit |
1470ea |
self.operation = ""
|
|
Packit |
1470ea |
# for a digit button
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
new_digit = int(button.get_label())
|
|
Packit |
1470ea |
if self.entry.get_text() == 'error':
|
|
Packit |
1470ea |
number = 0
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
number = int(self.entry.get_text())
|
|
Packit |
1470ea |
number = number * 10 + new_digit
|
|
Packit |
1470ea |
if self.counter == 0:
|
|
Packit |
1470ea |
self.first_number = number
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
self.second_number = number
|
|
Packit |
1470ea |
self.entry.set_text(str(number))
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
def do_operation(self):
|
|
Packit |
1470ea |
if self.operation == "plus":
|
|
Packit |
1470ea |
self.first_number += self.second_number
|
|
Packit |
1470ea |
elif self.operation == "minus":
|
|
Packit |
1470ea |
self.first_number -= self.second_number
|
|
Packit |
1470ea |
elif self.operation == "multiplication":
|
|
Packit |
1470ea |
self.first_number *= self.second_number
|
|
Packit |
1470ea |
elif self.operation == "division":
|
|
Packit |
1470ea |
try:
|
|
Packit |
1470ea |
self.first_number /= self.second_number
|
|
Packit |
1470ea |
except ZeroDivisionError:
|
|
Packit |
1470ea |
self.first_number = 0
|
|
Packit |
1470ea |
self.second_number = 0
|
|
Packit |
1470ea |
self.counter = 0
|
|
Packit |
1470ea |
self.entry.set_text('error')
|
|
Packit |
1470ea |
self.operation = ""
|
|
Packit |
1470ea |
return
|
|
Packit |
1470ea |
else:
|
|
Packit |
1470ea |
self.first_number = 0
|
|
Packit |
1470ea |
self.second_number = 0
|
|
Packit |
1470ea |
self.counter = 0
|
|
Packit |
1470ea |
self.entry.set_text('error')
|
|
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 |
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 ButtonBox</title>
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>La distribución de la botonera se configura con set_layout(distribución) , donde distribución puede ser Gtk.ButtonBoxStyle.SPREAD (los botones se separan equidistantes a lo largo de la botonera), Gtk.ButtonBoxStyle.EDGE (los botones se ponen en los bordes), Gtk.ButtonBoxStyle.START (los botones se agrupan hacia el comienzo de la botonera), Gtk.ButtonBoxStyle.END (los botones se agrupan hacia el final), Gtk.ButtonBoxStyle.CENTER (los botones se agrupan hacia el centro). </item>
|
|
Packit |
1470ea |
<item>set_child_secondary(botón, es_secundario) configura si botón debe aparecer en un grupo secundario de hijos. Un uso típico de un hijo secundario es el botón de ayuda en un diálogo. Este grupo aparece después de los otros hijos si el estilo es START , SPREAD o EDGE , y antes de los otros hijos si el estilo es END . Si el estilo es START o END , entonces los hijos secundarios se alinean al otro lado de los hijos principales en la botonera . Para los otros estilos, aparecen inmediatamente a continuación de los hijos principales. </item>
|
|
Packit |
1470ea |
<item>set_child_non_homogeneous(botón, es_heterogéneo) establece si el hijo está exento de dimensionado homogéneo. El valor predeterminado es False . </item>
|
|
Packit |
1470ea |
<item>set_spacing(espaciado) establece el espaciado, en píxeles, entre los botones de la botonera. </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
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/stable/GtkButtonBox.html">GtkButtonBox</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkBox.html">GtkBox</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkButton.html">GtkButton</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkEntry.html">GtkEntry</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gtk3/stable/GtkGrid.html">GtkGrid</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|