[gnome-devel-docs] tutorials python: new and improved buttonbox example and page
- From: Marta Maria Casetti <mmcasetti src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-devel-docs] tutorials python: new and improved buttonbox example and page
- Date: Thu, 16 Aug 2012 18:00:33 +0000 (UTC)
commit 41907a6d9d0355015d296f2933e5878b7372caf9
Author: Marta Maria Casetti <mmcasetti gmail com>
Date: Thu Aug 16 18:59:13 2012 +0100
tutorials python: new and improved buttonbox example and page
platform-demos/C/buttonbox.py.page | 14 ++-
platform-demos/C/media/buttonbox_calculator.png | Bin 5929 -> 10002 bytes
platform-demos/C/samples/buttonbox.py | 185 +++++++++++++++--------
3 files changed, 136 insertions(+), 63 deletions(-)
---
diff --git a/platform-demos/C/buttonbox.py.page b/platform-demos/C/buttonbox.py.page
index cdff805..ee9d31d 100644
--- a/platform-demos/C/buttonbox.py.page
+++ b/platform-demos/C/buttonbox.py.page
@@ -21,7 +21,7 @@
<title>ButtonBox</title>
<media type="image" mime="image/png" src="media/buttonbox_calculator.png"/>
- <p>Some labels in a grid.</p>
+ <p>A calculator - the buttons are enclosed in horizontal ButtonBoxes.</p>
<links type="section" />
@@ -30,13 +30,25 @@
<code mime="text/python" style="numbered"><xi:include href="samples/buttonbox.py" parse="text"><xi:fallback/></xi:include></code>
</section>
+ <section id="methods">
+ <title>Uselful methods for a ButtonBox widget</title>
+ <list>
+ <item><p>The layout of the ButtonBox are set with <code>set_layout(layout)</code>, where <code>layout</code> can be <code>Gtk.ButtonBoxStyle.SPREAD</code> (buttons are evenly spread across the box), <code>Gtk.ButtonBoxStyle.EDGE</code> (buttons are placed at the edges of the box), <code>Gtk.ButtonBoxStyle.START</code> (buttons are grouped towards the start of the box), <code>Gtk.ButtonBoxStyle.END</code> (buttons are grouped towards the end of the box), <code>Gtk.ButtonBoxStyle.CENTER</code> (buttons are centered in the box).</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> sets whether the child is exempted from homogeous sizing. Default value is <code>False</code>.</p></item>
+ <item><p><code>set_spacing(spacing)</code> sets the spacing, in pixels, between the buttons of the box.</p></item>
+ </list>
+ </section>
+
<section id="references">
<title>API References</title>
<p>In this sample we used the following:</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>
diff --git a/platform-demos/C/media/buttonbox_calculator.png b/platform-demos/C/media/buttonbox_calculator.png
index f110c4d..a82e7a5 100644
Binary files a/platform-demos/C/media/buttonbox_calculator.png and b/platform-demos/C/media/buttonbox_calculator.png differ
diff --git a/platform-demos/C/samples/buttonbox.py b/platform-demos/C/samples/buttonbox.py
index 1ed7872..484910e 100644
--- a/platform-demos/C/samples/buttonbox.py
+++ b/platform-demos/C/samples/buttonbox.py
@@ -4,82 +4,134 @@ import sys
class MyWindow(Gtk.ApplicationWindow):
def __init__(self, app):
Gtk.Window.__init__(self, title="Calculator", application=app)
- self.set_default_size(450, 100)
+ self.set_default_size(350, 200)
self.set_border_width(10)
- box = Gtk.ButtonBox.new(Gtk.Orientation.HORIZONTAL)
-
- button_plus = Gtk.Button(label="+")
- button_plus.connect("clicked", self.plus_callback)
- box.add(button_plus)
-
- button_minus = Gtk.Button(label="-")
- button_minus.connect("clicked", self.minus_callback)
- box.add(button_minus)
-
- button_multi = Gtk.Button(label="*")
- button_multi.connect("clicked", self.multi_callback)
- box.add(button_multi)
+ # an entry
+ self.entry = Gtk.Entry()
+ # with an initial text
+ self.entry.set_text('0')
+ # text aligned on the right
+ self.entry.set_alignment(1)
+ # the text in the entry cannot be modified writing in it
+ self.entry.set_can_focus(False)
+
+ # a grid
+ grid = Gtk.Grid()
+ grid.set_row_spacing(5)
- button_divided = Gtk.Button(label="/")
- button_divided.connect("clicked", self.divided_callback)
- box.add(button_divided)
+ # to attach the entry
+ grid.attach(self.entry, 0, 0, 1, 1)
- button_equal = Gtk.Button(label="=")
- button_equal.connect("clicked", self.equal_callback)
- box.add(button_equal)
-
- button_cancel = Gtk.Button(label="C")
- button_cancel.connect("clicked", self.cancel_callback)
- box.add(button_cancel)
+ # the labels for the buttons
+ buttons = [ 7, 8, 9, '/',
+ 4, 5, 6, '*',
+ 1, 2, 3, '-',
+ 'C', 0, '=', '+' ]
- box.set_child_non_homogeneous(button_cancel, True)
-
- self.entry = Gtk.Entry()
- self.entry.set_text('0.0')
- self.entry.connect("activate", self.equal_callback)
-
- self.second_number = 0.0
- self.result = 0.0
-
+ # each row is a ButtonBox, attached to the grid
+ for i in range(4):
+ hbox = Gtk.ButtonBox.new(Gtk.Orientation.HORIZONTAL)
+ hbox.set_spacing(5)
+ grid.attach(hbox, 0, i + 1, 1, 1)
+ # each ButtonBox has 4 buttons, connected to the callback function
+ 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)
+
+ # some variables for the calculations
+ self.first_number = 0
+ self.result = 0
+ self.counter = 0
self.operation = ""
- grid = Gtk.Grid()
- grid.attach(self.entry, 0, 0, 1, 1)
- grid.attach(box, 0, 1, 1, 1)
-
+ # add the grid to the window
self.add(grid)
-
+
+ # callback function for all the buttons
+ def button_clicked(self, button):
+ # functions for the operations
+ if button.get_label() == '+':
+ self.plus_callback(button)
+ elif button.get_label() == '-':
+ self.minus_callback(button)
+ elif button.get_label() == '*':
+ self.multi_callback(button)
+ elif button.get_label() == '/':
+ self.divided_callback(button)
+ # for =
+ elif button.get_label() == '=':
+ self.equal_callback(button)
+ # for Cancel
+ elif button.get_label() == 'C':
+ self.cancel_callback(button)
+ # for a digit button
+ 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
+ self.entry.set_text(str(number))
+
+ # to calculate +
def plus_callback(self, button):
- if self.entry.get_text() == "":
- self.first_number = 0.0
+ self.counter += 1
+ if self.counter == 1:
+ self.first_number = int(self.entry.get_text())
else:
- self.first_number = float(self.entry.get_text())
+ self.second_number = int(self.entry.get_text())
+ self.first_number = self.first_number + self.second_number
+ self.entry.set_text('0')
self.operation = "plus"
-
+
+ # to calculate -
def minus_callback(self, button):
- if self.entry.get_text() == "":
- self.first_number = 0.0
+ self.counter += 1
+ if self.counter == 1:
+ self.first_number = int(self.entry.get_text())
else:
- self.first_number = float(self.entry.get_text())
+ self.second_number = int(self.entry.get_text())
+ self.first_number = self.first_number - self.second_number
+ self.entry.set_text('0')
self.operation = "minus"
+ # to calculate *
def multi_callback(self, button):
- if self.entry.get_text() == "":
- self.first_number = 0.0
+ self.counter += 1
+ if self.counter == 1:
+ self.first_number = int(self.entry.get_text())
else:
- self.first_number = float(self.entry.get_text())
+ self.second_number = int(self.entry.get_text())
+ self.first_number = self.first_number * self.second_number
+ self.entry.set_text('0')
self.operation = "multiplication"
+ # to calculate /
def divided_callback(self, button):
- if self.entry.get_text() == "":
- self.first_number = 0.0
+ self.counter += 1
+ if self.counter == 1:
+ self.first_number = int(self.entry.get_text())
else:
- self.first_number = float(self.entry.get_text())
+ self.second_number = int(self.entry.get_text())
+ try:
+ self.first_number = self.first_number / self.second_number
+ except ZeroDivisionError:
+ self.cancel_callback(button)
+ self.entry.set_text("error")
+ self.first_number = 0
+ self.second_number = 0
+ self.counter = 0
+ return
+ self.entry.set_text('0')
self.operation = "division"
+ # to return a result (=)
def equal_callback(self, button):
- self.second_number = float(self.entry.get_text())
+ self.second_number = int(self.entry.get_text())
if self.operation == "plus":
self.result = self.first_number + self.second_number
elif self.operation == "minus":
@@ -87,21 +139,30 @@ class MyWindow(Gtk.ApplicationWindow):
elif self.operation == "multiplication":
self.result = self.first_number * self.second_number
elif self.operation == "division":
- self.result = self.first_number / self.second_number
+ try:
+ self.result = self.first_number / self.second_number
+ except ZeroDivisionError:
+ self.cancel_callback(button)
+ self.entry.set_text("error")
+ self.first_number = 0
+ self.second_number = 0
+ self.counter = 0
+ return
else:
- print "Error!"
+ self.result = 0
self.first_number = self.result
+ self.second_number = 0
+ self.counter = 0
self.entry.set_text(str(self.result))
+ # to cancel the operation
def cancel_callback(self, button):
- if self.entry.get_text() == "":
- self.first_number = 0.0
- else:
- self.first_number = float(self.entry.get_text())
- self.second_number = 0.0
- self.result = 0.0
- self.entry.set_text('0.0')
-
+ self.first_number = 0
+ self.second_number = 0
+ self.result = 0
+ self.counter = 0
+ self.entry.set_text('0')
+
class MyApplication(Gtk.Application):
def __init__(self):
Gtk.Application.__init__(self)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]