-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
71 lines (54 loc) · 2.9 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
71
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.config import Config
Config.set("graphics", "resizable", "1")
Config.set("graphics", "width", "300")
Config.set("graphics", "height", "300")
saveInput = ""
class CalculatorApp(App):
def calculate(self, symbol):
global saveInput
if symbol.text is '<--':
saveInput = self.result.text = ""
elif symbol.text is not '=':
self.result.text += symbol.text
saveInput += symbol.text
else:
try:
saveInput = self.result.text = str(eval(saveInput))
except:
saveInput = self.result.text = ""
def build(self):
root = BoxLayout(orientation="vertical", padding=5)
self.result = TextInput(
text="", readonly=True, font_size=50,
size_hint=[1, .75], background_color=[1, 1, 1, .8])
root.add_widget(self.result)
allButtons = GridLayout(cols=4)
allButtons.add_widget(Button(text="%", font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text="(", font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text=")", font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text="/", font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text='7', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text='8', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text='9', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text="*", font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text='4', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text='5', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text='6', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text="+", font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text='1', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text='2', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text='3', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text="-", font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text=".", font_size=40, on_press=self.calculate))
allButtons.add_widget(Button(text='0', font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text="<--", font_size=30, on_press=self.calculate))
allButtons.add_widget(Button(text="=", font_size=50, on_press=self.calculate))
root.add_widget(allButtons)
return root
if __name__ == "__main__":
CalculatorApp().run()