-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
118 lines (93 loc) · 3.85 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# ---------- main.py ----------
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
import kivy
kivy.require('1.10.0')
class CalcGridLayout(GridLayout):
# Function called when equals is pressed
def calculate(self, calculation):
if calculation:
string = calculation
try:
# Solve formula and display it in entry
# which is pointed at by display
self.display.text = str(eval(string.replace('&&', 'and').replace('||', 'or').replace('!', 'not ')))
except Exception:
self.display.text = "Error"
class RootWidget(BoxLayout):
'''Create a controller that receives a custom widget from the kv lang file.
Add an action to be called from a kv file.
'''
container = ObjectProperty(None)
class NumConv(BoxLayout):
def num_conversion(self, input1, output, data):
try:
if input1 == 'Decimal':
if output == 'Decimal':
self.display.text = str(int(data))
elif output == 'Hexadecimal':
self.display.text = str(hex(int(data)))
elif output == 'Octal':
self.display.text = str(oct(int(data)))
elif output == 'Binary':
self.display.text = str(bin(int(data)))
if input1 == 'Hexadecimal':
if output == 'Decimal':
self.display.text = str(int(data, 16))
elif output == 'Hexadecimal':
self.display.text = str(hex(int(data, 16)))
elif output == 'Octal':
self.display.text = str(oct(int(data, 16)))
elif output == 'Binary':
self.display.text = str(bin(int(data, 16)))
if input1 == 'Octal':
if output == 'Decimal':
self.display.text = str(int(data, 8))
elif output == 'Hexadecimal':
self.display.text = str(hex(int(data, 8)))
elif output == 'Octal':
self.display.text = str(oct(int(data, 8)))
elif output == 'Binary':
self.display.text = str(bin(int(data, 8)))
if input1 == 'Binary':
if output == 'Decimal':
self.display.text = str(int(data, 2))
elif output == 'Hexadecimal':
self.display.text = str(hex(int(data, 2)))
elif output == 'Octal':
self.display.text = str(oct(int(data, 2)))
elif output == 'Binary':
self.display.text = str(bin(int(data, 2)))
except Exception:
self.display.text = "Error"
class CalculatorApp(App):
'''This is the app itself'''
def build(self):
'''This method loads the root.kv file automatically
:return: none
'''
# loading the content of root.kv
self.root = Builder.load_file('kv_modes/root.kv')
def screen_select(self, selector):
'''
:param selector: character that is dependant on which mode button is toggled.
:return:
'''
filename = selector + '.kv'
# unload the content of the .kv file
# reason: may have data from previous calls
Builder.unload_file('kv_modes/' + filename)
# clear the container
self.root.container.clear_widgets()
# load the .kv file
selector = Builder.load_file('kv_modes/' + filename)
# add content of the .kv file to the container
self.root.container.add_widget(selector)
if __name__ == '__main__':
'''Start the application'''
CalculatorApp().run()