Blame platform-demos/pt_BR/buttonbox.py.page

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="pt-BR">
Packit 1470ea
  <info>
Packit 1470ea
    <title type="text">ButtonBox (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>A container for arranging buttons</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Rafael Ferreira</mal:name>
Packit 1470ea
      <mal:email>rafael.f.f1@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2013</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>ButtonBox</title>
Packit 1470ea
Packit 1470ea
  <media type="image" mime="image/png" src="media/buttonbox_calculator.png"/>
Packit 1470ea
  

A calculator - the buttons are enclosed in horizontal ButtonBoxes.

Packit 1470ea
Packit 1470ea
  <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="code">
Packit 1470ea
    <title>Code used to generate this example</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>Useful methods for a ButtonBox widget</title>
Packit 1470ea
    <list>
Packit 1470ea
      <item>

The layout of the ButtonBox are set with set_layout(layout), where layout can be Gtk.ButtonBoxStyle.SPREAD (buttons are evenly spread across the box), Gtk.ButtonBoxStyle.EDGE (buttons are placed at the edges of the box), Gtk.ButtonBoxStyle.START (buttons are grouped towards the start of the box), Gtk.ButtonBoxStyle.END (buttons are grouped towards the end of the box), Gtk.ButtonBoxStyle.CENTER (buttons are centered in the box).

</item>
Packit 1470ea
      <item>

set_child_secondary(button, is_secondary) sets whether button should appear in a secondary group of children. A typical use of a secondary child is the help button in a dialog. This group appears after the other children if the style is START, SPREAD or EDGE, and before the other children if the style is END. If the style is START or END, then the secondary children are aligned at the other end of the button box from the main children. For the other styles, they appear immediately next to the main children.

</item>
Packit 1470ea
      <item>

set_child_non_homogeneous(button, is_non_homogeneous) sets whether the child is exempted from homogeneous sizing. Default value is False.

</item>
Packit 1470ea
      <item>

set_spacing(spacing) sets the spacing, in pixels, between the buttons of the box.

</item>
Packit 1470ea
    </list>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="references">
Packit 1470ea
    <title>API References</title>
Packit 1470ea
    

In this sample we used the following:

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>