#!/usr/bin/env python # Copyright (C) 2013 Cristhofer Travieso # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA from gi.repository import Gtk class Canvas(Gtk.VBox): def __init__(self): Gtk.VBox.__init__(self) self.entry = Gtk.Entry() self.add(self.entry) table = Gtk.Table() self.add(table) b0 = Gtk.Button('0') b0.connect('clicked', self.add_character) b00 = Gtk.Button('00') b00.connect('clicked', self.add_character) b1 = Gtk.Button('1') b1.connect('clicked', self.add_character) b2 = Gtk.Button('2') b2.connect('clicked', self.add_character) b3 = Gtk.Button('3') b3.connect('clicked', self.add_character) b4 = Gtk.Button('4') b4.connect('clicked', self.add_character) b5 = Gtk.Button('5') b5.connect('clicked', self.add_character) b6 = Gtk.Button('6') b6.connect('clicked', self.add_character) b7 = Gtk.Button('7') b7.connect('clicked', self.add_character) b8 = Gtk.Button('8') b8.connect('clicked', self.add_character) b9 = Gtk.Button('9') b9.connect('clicked', self.add_character) b_dot = Gtk.Button('.') b_dot.connect('clicked', self.add_character) b_plus = Gtk.Button('+') b_plus.connect('clicked', self.add_character) b_minus = Gtk.Button('-') b_minus.connect('clicked', self.add_character) b_times = Gtk.Button('*') b_times.connect('clicked', self.add_character) b_divided = Gtk.Button('/') b_divided.connect('clicked', self.add_character) table.attach(b7, 0, 1, 0, 1) table.attach(b4, 0, 1, 1, 2) table.attach(b1, 0, 1, 2, 3) table.attach(b0, 0, 1, 3, 4) table.attach(b8, 1, 2, 0, 1) table.attach(b5, 1, 2, 1, 2) table.attach(b2, 1, 2, 2, 3) table.attach(b_dot, 1, 2, 3, 4) table.attach(b9, 2, 3, 0, 1) table.attach(b6, 2, 3, 1, 2) table.attach(b3, 2, 3, 2, 3) table.attach(b00, 2, 3, 3, 4) table.attach(b_divided, 3, 4, 0, 1) table.attach(b_times, 3, 4, 1, 2) table.attach(b_minus, 3, 4, 2, 3) table.attach(b_plus, 3, 4, 3, 4) equals_button = Gtk.Button('=') equals_button.connect('clicked', self.work) self.add(equals_button) def add_character(self, widget): text = widget.get_label() text_a = self.entry.get_text() text_end = text_a + text self.entry.set_text(text_end) def work(self, widget): entry_text = self.entry.get_text() result = eval(entry_text) self.entry.set_text(str(result))