|
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="cs">
|
|
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>Kontejner pro uspořádání tlačítek</desc>
|
|
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 |
Kalkulačka – tlačítka jsou umístěna do vodorovných widgetů ButtonBox.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<links type="section"/>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="code">
|
|
Packit |
1470ea |
<title>Kód použitý k vygenerování tohoto příkladu</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 |
# Vstupní pole
|
|
Packit |
1470ea |
self.entry = Gtk.Entry()
|
|
Packit |
1470ea |
# s počátečním textem
|
|
Packit |
1470ea |
self.entry.set_text('0')
|
|
Packit |
1470ea |
# zarovnaným doprava
|
|
Packit |
1470ea |
self.entry.set_alignment(1)
|
|
Packit |
1470ea |
# Text v poli nemůže bý změněn psaním
|
|
Packit |
1470ea |
self.entry.set_can_focus(False)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Mřížka
|
|
Packit |
1470ea |
grid = Gtk.Grid()
|
|
Packit |
1470ea |
grid.set_row_spacing(5)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# do které se připojí vstupní pole
|
|
Packit |
1470ea |
grid.attach(self.entry, 0, 0, 1, 1)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Popisky pro tlačítka
|
|
Packit |
1470ea |
buttons = [7, 8, 9, '/',
|
|
Packit |
1470ea |
4, 5, 6, '*',
|
|
Packit |
1470ea |
1, 2, 3, '-',
|
|
Packit |
1470ea |
'C', 0, '=', '+']
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Každý z řádků je ButtonBox připojený do mřížky
|
|
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 |
# Každý ButtonBox má 4 tlačíka napojená na funkci zpětného volání
|
|
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 |
# Pár proměnných pro výpočty
|
|
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 |
# Přidá mřížku do okna
|
|
Packit |
1470ea |
self.add(grid)
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
# Funkce zpětného volání pro všechna tlačítka
|
|
Packit |
1470ea |
def button_clicked(self, button):
|
|
Packit |
1470ea |
# Pro početní operace
|
|
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 |
# Pro =
|
|
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 |
# Pro Zrušit
|
|
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 |
# Pro číslice
|
|
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>Užitečné metody pro widget ButtonBox</title>
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item>Rozvržení widgetu ButtonBox se nastavuje pomocí set_layout(rozvržení) , kde rozvržení může být Gtk.ButtonBoxStyle.SPREAD (tlačítka jsou v boxu rovnoměrně rozložená), Gtk.ButtonBoxStyle.EDGE (tlačítka jsou umístěna u hran boxu), Gtk.ButtonBoxStyle.START (tlačítka jsou seskupena na začátku boxu), Gtk.ButtonBoxStyle.END (tlačítka jsou seskupena na konci boxu), Gtk.ButtonBoxStyle.CENTER (tlačítka jsou na středu boxu). </item>
|
|
Packit |
1470ea |
<item>set_child_secondary(tlačítko, je_druhé) nastavuje, že tlačítko se má objevit v druhé skupině potomků. Typické použití jako druhého tlačítka je tlačítko nápověda v dialogovém okně. Tato skupina se objeví za ostatními potomky v případě, že styl je START , SPREAD or EDGE , a nebo před ostatními potomky, v případě že styl je END . Pokud je styl START nebo END , je druhý potomek zarovnán k opačnému konci tlačítkového boxu než hlavní potomek. Pro ostatní styly se objeví hned vedle hlavního potomka. </item>
|
|
Packit |
1470ea |
<item>set_child_non_homogeneous(tlačítko, není_homogení) nastavuje, že potomek je vynechán z homogenního nastavení velikosti. Výchozí hodnota je False . </item>
|
|
Packit |
1470ea |
<item>set_spacing(rozestup) nastaví rozestup, v pixelech, mezi tlačítky v boxu. </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="references">
|
|
Packit |
1470ea |
<title>Odkazy k API</title>
|
|
Packit |
1470ea |
V této ukázce se používá následující:
|
|
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>
|