-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
70 lines (55 loc) · 2.05 KB
/
calculator.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.lang.builder import Builder
import re
Builder.load_file('./calculator.kv')
Window.size = (350, 550)
class CalculatorWidget(Widget):
def clear(self):
self.ids.input_box.text = '0'
def button_value(self, number):
prev_number = self.ids.input_box.text
if "Wrong Equation" in prev_number:
prev_number = ''
if prev_number == '0':
self.ids.input_box.text = ''
self.ids.input_box.text = f"{number}"
else:
self.ids.input_box.text = f"{prev_number}{number}"
def signs(self, sign):
prev_number = self.ids.input_box.text
self.ids.input_box.text = f"{prev_number}{sign}"
def remove_last(self):
prev_number = self.ids.input_box.text
prev_number = prev_number[:-1]
self.ids.input_box.text = prev_number
def results(self):
prev_number = self.ids.input_box.text
try:
result = eval(prev_number)
self.ids.input_box.text = str(result)
except:
self.ids.input_box.text = "Wrong Equation"
def positive_negative(self):
prev_number = self.ids.input_box.text
if "-" in prev_number:
self.ids.input_box.text = f"{prev_number.replace('-', '')}"
else:
self.ids.input_box.text = f"-{prev_number}"
def dot(self):
prev_number = self.ids.input_box.text
num_list = re.split("\+|\*|-|/|%", prev_number)
if ("+" in prev_number or "-" in prev_number or "/" in prev_number or "*" in prev_number or "%" in prev_number) and "." not in num_list[-1]:
prev_number = f"{prev_number}."
self.ids.input_box.text = prev_number
elif "." in prev_number:
pass
else:
prev_number = f"{prev_number}."
self.ids.input_box.text = prev_number
class CalculatorApp(App):
def build(self):
return CalculatorWidget()
if __name__ == "__main__":
CalculatorApp().run()