<?xml version="1.0" encoding="utf-8"?>
<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" lang="sv">
  <info>
    <title type="text">ButtonBox (Python)</title>
    <link type="guide" xref="beginner.py#layout"/>
    <link type="seealso" xref="button.py"/>
    <link type="next" xref="statusbar.py"/>
    <revision version="0.2" date="2012-08-01" status="stub"/>

    <credit type="author copyright">
      <name>Marta Maria Casetti</name>
      <email its:translate="no">mmcasetti@gmail.com</email>
      <years>2012</years>
    </credit>

    <desc>En behållare för att arrangera knappar</desc>
  <credit type="translator copyright"><name>Sebastian Rasmussen</name><email>sebras@gmail.com</email><years>2019</years></credit><credit type="translator copyright"><name>Anders Jonsson</name><email>anders.jonsson@norsjovallen.se</email><years>2021</years></credit></info>

  <title>ButtonBox</title>

  <media type="image" mime="image/png" src="media/buttonbox_calculator.png"/>
  <p>En kalkylator - knapparna infogas i horisontella ButtonBoxar.</p>

  <links type="section"/>

  <section id="code">
    <title>Kod som använts för att generera detta exempel</title>
    <code mime="text/python" style="numbered">from gi.repository import Gtk
import sys


class MyWindow(Gtk.ApplicationWindow):

    def __init__(self, app):
        Gtk.Window.__init__(self, title="Kalkylator", application=app)
        self.set_default_size(350, 200)
        self.set_border_width(10)

        # ett fält
        self.entry = Gtk.Entry()
        # med en initial text
        self.entry.set_text('0')
        # text högerjusterad
        self.entry.set_alignment(1)
        # texten i fältet kan inte ändras genom att skriva i det
        self.entry.set_can_focus(False)

        # ett rutnät
        grid = Gtk.Grid()
        grid.set_row_spacing(5)

        # för att fästa rutnätet
        grid.attach(self.entry, 0, 0, 1, 1)

        # etiketterna för knapparna
        buttons = [7, 8, 9, '/',
                   4, 5, 6, '*',
                   1, 2, 3, '-',
                   'C', 0, '=', '+']

        # varje rad är en ButtonBox, fäst vid rutnätet
        for i in range(4):
            hbox = Gtk.ButtonBox.new(Gtk.Orientation.HORIZONTAL)
            hbox.set_spacing(5)
            grid.attach(hbox, 0, i + 1, 1, 1)
            # varje ButtonBox har 4 knappar, anslutna till återanropsfunktionen
            for j in range(4):
                button = Gtk.Button(label=buttons[i * 4 + j])
                button.set_can_focus(False)
                button.connect("clicked", self.button_clicked)
                hbox.add(button)

        # några variabler för beräkningarna
        self.first_number = 0
        self.second_number = 0
        self.counter = 0
        self.operation = ""

        # lägg till rutnätet till fönstret
        self.add(grid)

    # återanropsfunktion för alla knapparna
    def button_clicked(self, button):
        # för operationerna
        if button.get_label() == '+':
            self.counter += 1
            if self.counter &gt; 1:
                self.do_operation()
            self.entry.set_text('0')
            self.operation = "plus"
        elif button.get_label() == '-':
            self.counter += 1
            if self.counter &gt; 1:
                self.do_operation()
            self.entry.set_text('0')
            self.operation = "minus"
        elif button.get_label() == '*':
            self.counter += 1
            if self.counter &gt; 1:
                self.do_operation()
            self.entry.set_text('0')
            self.operation = "multiplication"
        elif button.get_label() == '/':
            self.counter += 1
            if self.counter &gt; 1:
                self.do_operation()
            self.entry.set_text('0')
            self.operation = "division"
        # för =
        elif button.get_label() == '=':
            self.do_operation()
            self.entry.set_text(str(self.first_number))
            self.counter = 1
        # för avbryt (C)
        elif button.get_label() == 'C':
            self.first_number = 0
            self.second_number = 0
            self.counter = 0
            self.entry.set_text('')
            self.operation = ""
        # för en sifferknapp
        else:
            new_digit = int(button.get_label())
            if self.entry.get_text() == 'error':
                number = 0
            else:
                number = int(self.entry.get_text())
            number = number * 10 + new_digit
            if self.counter == 0:
                self.first_number = number
            else:
                self.second_number = number
            self.entry.set_text(str(number))

    def do_operation(self):
        if self.operation == "plus":
            self.first_number += self.second_number
        elif self.operation == "minus":
            self.first_number -= self.second_number
        elif self.operation == "multiplication":
            self.first_number *= self.second_number
        elif self.operation == "division":
            try:
                self.first_number /= self.second_number
            except ZeroDivisionError:
                self.first_number = 0
                self.second_number = 0
                self.counter = 0
                self.entry.set_text('error')
                self.operation = ""
                return
        else:
            self.first_number = 0
            self.second_number = 0
            self.counter = 0
            self.entry.set_text('error')


class MyApplication(Gtk.Application):

    def __init__(self):
        Gtk.Application.__init__(self)

    def do_activate(self):
        win = MyWindow(self)
        win.show_all()

    def do_startup(self):
        Gtk.Application.do_startup(self)

app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)
</code>
  </section>

  <section id="methods">
    <title>Användbara metoder för en ButtonBox-komponent</title>
    <list>
      <item><p>Layouten för ButtonBoxen ställs in med <code>set_layout(layout)</code>, där <code>layout</code> kan vara <code>Gtk.ButtonBoxStyle.SPREAD</code> (knappar fördelas jämnt över rutan), <code>Gtk.ButtonBoxStyle.EDGE</code> (knappar placeras i rutans kanter), <code>Gtk.ButtonBoxStyle.START</code> (knappar grupperas mot starten på rutan), <code>Gtk.ButtonBoxStyle.END</code> (knappar grupperas mot slutet på rutan), <code>Gtk.ButtonBoxStyle.CENTER</code> (knappar centreras i rutan).</p></item>
      <item><p><code>set_child_secondary(button, is_secondary)</code> sets whether <code>button</code> 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 <code>START</code>, <code>SPREAD</code> or <code>EDGE</code>, and before the other children if the style is <code>END</code>. If the style is <code>START</code> or <code>END</code>, 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.</p></item>
      <item><p><code>set_child_non_homogeneous(button, is_non_homogeneous)</code> ställer in huruvida barnet undantas från homogen omformning. Standardvärdet är <code>False</code>.</p></item>
      <item><p><code>set_spacing(utrymme)</code> ställer in utrymmet, i bildpunkter, mellan rutans knappar.</p></item>
    </list>
  </section>

  <section id="references">
    <title>API-referenser</title>
    <p>I detta exempel använde vi följande:</p>
    <list>
      <item><p><link href="http://developer.gnome.org/gtk3/stable/GtkButtonBox.html">GtkButtonBox</link></p></item>
      <item><p><link href="http://developer.gnome.org/gtk3/stable/GtkBox.html">GtkBox</link></p></item>
      <item><p><link href="http://developer.gnome.org/gtk3/stable/GtkButton.html">GtkButton</link></p></item>
      <item><p><link href="http://developer.gnome.org/gtk3/stable/GtkEntry.html">GtkEntry</link></p></item>
      <item><p><link href="http://developer.gnome.org/gtk3/stable/GtkGrid.html">GtkGrid</link></p></item>
    </list>
  </section>
</page>
