Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/canvas.py
blob: 14db57ebbe6ab298082937e046cd60a5b0008c2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python

# Copyright (C) 2012 Cristhofer Travieso <cristhofert97@gmail.com>

# 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))