-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnumbers.py
executable file
·49 lines (39 loc) · 1.56 KB
/
numbers.py
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
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class NumbersGrid(Gtk.Grid):
def __init__(self):
Gtk.Grid.__init__(self)
self.column_homogeneous=True
self.numbers = []
self.create_numbers_grid()
self.create_lastline()
def create_numbers_grid(self):
for i in range(3):
for j in range(3):
self.numbers.append(Gtk.Button(label='{}'.format(3*i+j+1)))
self.attach(self.numbers[3*i+j], j, i, 1, 1)
def create_lastline(self):
zero_button = Gtk.Button(label='0')
comma_button = Gtk.Button(label='.')
plus_minus_button = Gtk.Button(label='±')
self.numbers.append(zero_button)
self.numbers.append(comma_button)
self.numbers.append(plus_minus_button)
self.attach(zero_button, 0, 3, 1, 1)
self.attach(comma_button, 1, 3, 1, 1)
self.attach(plus_minus_button, 2, 3, 1, 1)
def connect_to_display(self, display):
#connect all grid buttons to main display
self.display = display
for button in self.numbers:
button.connect('clicked', self.on_number_clicked, self.display)
def on_number_clicked(self, widget, display):
old_txt = display.get_text()
to_add = widget.get_label()
new_txt = old_txt + to_add
if new_txt == '.' or new_txt == '±':
new_txt = new_txt[:-1]
if (to_add == '.' or to_add == '±') and new_txt.count(to_add) > 1:
new_txt = new_txt[:-1]
display.set_text(new_txt)