-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcompleteApp_part2.py
134 lines (122 loc) · 4.85 KB
/
completeApp_part2.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from kivymd.uix.screen import MDScreen
from kivymd.app import MDApp
from kivy.uix.image import Image
from kivymd.uix.button import MDFillRoundFlatIconButton, MDFillRoundFlatButton
from kivymd.uix.textfield import MDTextField
from kivymd.uix.label import MDLabel
from kivymd.uix.toolbar import MDToolbar
class ConverterApp(MDApp):
def flip(self):
# a method for the "flip" icon
# changes the state of the app
if self.state == 0:
self.state = 1
self.toolbar.title = "Decimal to Binary"
self.input.text = "enter a decimal number"
else:
self.state = 0
self.toolbar.title = "Binary to Decimal"
self.input.text = "enter a binary number"
# hide labels until needed
self.converted.text = ""
self.label.text = ""
def convert(self, args):
# a method to find the decimal/binary equivallent
try:
if "." not in self.input.text:
# if the user-provided number is not a fraction
if self.state == 0:
# binary to decimal
val = str(int(self.input.text,2))
self.label.text = "in decimal is:"
else:
# decimal to binary
val = bin(int(self.input.text))[2:]
self.label.text = "in binary is:"
self.converted.text = val
else:
#if the user provided number is a fraction
whole, fract = self.input.text.split(".")
if self.state == 0:
#convert binary to decimal
whole = int(whole, 2)
floating = 0
for idx, digit in enumerate(fract):
floating += int(digit)*2**(-(idx+1))
self.label.text = "in decimal is:"
self.converted.text = str(whole + floating)
else:
#convert decimal to binary
decimal_places = 10
whole = bin(int(whole))[2:]
fract = float("0."+fract)
floating = []
for i in range(decimal_places):
if fract*2 < 1:
floating.append("0")
fract *= 2
elif fract*2 > 1:
floating.append("1")
fract = fract*2 - 1
elif fract*2 == 1.0:
floating.append("1")
break
self.label.text = "in binary is:"
self.converted.text = whole + "." + "".join(floating)
except ValueError:
#if the user-provided value is invalid
self.converted.text = ""
if self.state == 0:
#binary to decimal
self.label.text = "please enter a valid binary number"
else:
#decimal to binary
self.label.text = "please enter a valid decimal number"
def build(self):
self.state = 0 #initial state
#self.theme_cls.primary_palette = "DeepOrange"
screen = MDScreen()
# top toolbar
self.toolbar = MDToolbar(title="Binary to Decimal")
self.toolbar.pos_hint = {"top": 1}
self.toolbar.right_action_items = [
["rotate-3d-variant", lambda x: self.flip()]]
screen.add_widget(self.toolbar)
# logo
screen.add_widget(Image(
source="logo.png",
pos_hint = {"center_x": 0.5, "center_y":0.7}
))
#collect user input
self.input = MDTextField(
text="enter a binary number",
halign="center",
size_hint = (0.8,1),
pos_hint = {"center_x": 0.5, "center_y":0.5},
font_size = 22
)
screen.add_widget(self.input)
#secondary + primary labels
self.label = MDLabel(
halign="center",
pos_hint = {"center_x": 0.5, "center_y":0.35},
theme_text_color = "Secondary"
)
self.converted = MDLabel(
halign="center",
pos_hint = {"center_x": 0.5, "center_y":0.3},
theme_text_color = "Primary",
font_style = "H5"
)
screen.add_widget(self.label)
screen.add_widget(self.converted)
# "CONVERT" button
screen.add_widget(MDFillRoundFlatButton(
text="CONVERT",
font_size = 17,
pos_hint = {"center_x": 0.5, "center_y":0.15},
on_press = self.convert
))
return screen
if __name__ == '__main__':
ConverterApp().run()