1
1
from tkinter import *
2
+
2
3
window = Tk ()
3
4
window .title ("Standard Binary Calculator" )
4
5
window .resizable (0 , 0 )
5
6
6
-
7
+ # Function to clear the entry field
7
8
def f1 ():
8
9
s = e1_val .get ()
9
10
e1 .delete (first = 0 , last = len (s ))
10
11
11
-
12
+ # Function to add "1" to the entry field
12
13
def f2 ():
13
14
s = e1_val .get ()
14
15
e1 .insert (END , "1" )
15
16
16
-
17
+ # Function to add "0" to the entry field
17
18
def f3 ():
18
19
s = e1_val .get ()
19
20
e1 .insert (END , "0" )
20
21
21
-
22
+ # Function to evaluate the expression and display the result
22
23
def f4 ():
23
- x = 0
24
24
s = e1_val .get ()
25
- for i in range (0 , len (s )):
26
- if s [i ] == '/' or s [i ] == 'X' or s [i ] == '+' or s [i ] == '-' :
27
- a = s [0 :i ]
28
- b = s [i + 1 :len (s )]
29
- if s [i ] == '-' :
30
- x = sub (int (a ), int (b ))
31
- elif s [i ] == '/' :
32
- x = int (int (a ) / int (b ))
33
- elif s [i ] == 'X' :
34
- x = int (int (a ) * int (b ))
35
- elif s [i ] == '+' :
36
- x = int (add (int (a ), int (b )))
37
-
25
+ result = evaluate_expression (s )
38
26
e1 .delete (first = 0 , last = len (s ))
39
- e1 .insert (END , "" )
40
- e1 .insert (END , str (x ))
27
+ e1 .insert (END , result )
28
+
29
+ # Function to add "+" operator to the entry field
30
+ def f5 ():
31
+ e1 .insert (END , "+" )
32
+
33
+ # Function to add "-" operator to the entry field
34
+ def f6 ():
35
+ e1 .insert (END , "-" )
36
+
37
+ # Function to add "/" operator to the entry field
38
+ def f7 ():
39
+ e1 .insert (END , "/" )
41
40
41
+ # Function to add "X" operator to the entry field
42
+ def f8 ():
43
+ e1 .insert (END , "X" )
44
+
45
+ # Function to negate the sign of the number in the entry field
46
+ def negate ():
47
+ s = e1_val .get ()
48
+ if s .startswith ("-" ):
49
+ e1_val .set (s [1 :])
50
+ else :
51
+ e1_val .set ("-" + s )
42
52
43
- def bin_to_dec (n ):
53
+ # Function to delete the last character in the entry field
54
+ def backspace ():
55
+ s = e1_val .get ()
56
+ e1_val .set (s [:- 1 ])
57
+
58
+ # Function to convert binary to decimal
59
+ def binary_to_decimal (n ):
44
60
num = n
45
61
dec_value = 0
46
62
base = 1
47
63
temp = num
48
- while ( temp ) :
64
+ while temp :
49
65
last_digit = temp % 10
50
- temp = int (temp / 10 )
51
-
66
+ temp = temp // 10
52
67
dec_value += last_digit * base
53
68
base = base * 2
54
69
return dec_value
55
70
56
-
57
- def add (x , y ):
58
- a = bin_to_dec (x )
59
- b = bin_to_dec (y )
60
- c = a + b
61
- d = bin (c ).replace ("0b" , "" )
62
- return d
63
-
64
-
65
- def sub (x , y ):
66
- a = bin_to_dec (x )
67
- b = bin_to_dec (y )
68
- c = a - b
69
- d = bin (c ).replace ("0b" , "" )
70
- return d
71
-
72
-
73
- def f5 ():
74
- x = 0
75
- s = e1_val .get ()
76
- flag = 1
77
- for i in range (0 , len (s )):
78
- if s [i ] == '/' or s [i ] == 'X' or s [i ] == '+' or s [i ] == '-' :
79
- flag = 0
80
- a = s [0 :i ]
81
- b = s [i + 1 :len (s )]
82
- if s [i ] == '-' :
83
- x = sub (int (a ), int (b ))
84
- elif s [i ] == '/' :
85
- x = int (int (a ) / int (b ))
86
- elif s [i ] == 'X' :
87
- x = int (int (a ) * int (b ))
88
- elif s [i ] == '+' :
89
- x = int (add (int (a ), int (b )))
90
- if flag == 0 :
91
- e1 .delete (first = 0 , last = len (s ))
92
- e1 .insert (END , str (x ))
93
- e1 .insert (END , "+" )
94
-
95
-
96
- def f6 ():
97
- x = 0
98
- s = e1_val .get ()
99
- flag = 1
100
- for i in range (0 , len (s )):
101
- if s [i ] == '/' or s [i ] == 'X' or s [i ] == '+' or s [i ] == '-' :
102
- flag = 0
103
- a = s [0 :i ]
104
- b = s [i + 1 :len (s )]
105
- if s [i ] == '-' :
106
- x = sub (int (a ), int (b ))
107
- elif s [i ] == '/' :
108
- x = int (int (a ) / int (b ))
109
- elif s [i ] == 'X' :
110
- x = int (int (a ) * int (b ))
111
- elif s [i ] == '+' :
112
- x = int (add (int (a ), int (b )))
113
- if flag == 0 :
114
- e1 .delete (first = 0 , last = len (s ))
115
- e1 .insert (END , str (x ))
116
- e1 .insert (END , "-" )
117
-
118
-
119
- def f7 ():
71
+ # Function to convert decimal to binary
72
+ def decimal_to_binary (n ):
73
+ if n == 0 :
74
+ return '0'
75
+ binary = ''
76
+ while n > 0 :
77
+ binary = str (n % 2 ) + binary
78
+ n = n // 2
79
+ return binary
80
+
81
+ # Function to evaluate the expression
82
+ def evaluate_expression (expression ):
120
83
x = 0
121
- s = e1_val . get ()
84
+ s = expression
122
85
flag = 1
123
86
for i in range (0 , len (s )):
124
87
if s [i ] == '/' or s [i ] == 'X' or s [i ] == '+' or s [i ] == '-' :
@@ -134,50 +97,48 @@ def f7():
134
97
elif s [i ] == '+' :
135
98
x = int (add (int (a ), int (b )))
136
99
if flag == 0 :
137
- e1 .delete (first = 0 , last = len (s ))
138
- e1 .insert (END , str (x ))
139
- e1 .insert (END , "/" )
100
+ return str (x )
101
+ return ""
140
102
103
+ # Function to perform binary addition
104
+ def add (x , y ):
105
+ a = binary_to_decimal (x )
106
+ b = binary_to_decimal (y )
107
+ c = a + b
108
+ d = decimal_to_binary (c )
109
+ return d
141
110
142
- def f8 ():
143
- x = 0
144
- s = e1_val .get ()
145
- flag = 1
146
- for i in range (0 , len (s )):
147
- if s [i ] == '/' or s [i ] == 'X' or s [i ] == '+' or s [i ] == '-' :
148
- flag = 0
149
- a = s [0 :i ]
150
- b = s [i + 1 :len (s )]
151
- if s [i ] == '-' :
152
- x = sub (int (a ), int (b ))
153
- elif s [i ] == '/' :
154
- x = int (int (a ) / int (b ))
155
- elif s [i ] == 'X' :
156
- x = int (int (a ) * int (b ))
157
- elif s [i ] == '+' :
158
- x = int (add (int (a ), int (b )))
159
- if flag == 0 :
160
- e1 .delete (first = 0 , last = len (s ))
161
- e1 .insert (END , str (x ))
162
- e1 .insert (END , "X" )
163
-
111
+ # Function to perform binary subtraction
112
+ def sub (x , y ):
113
+ a = binary_to_decimal (x )
114
+ b = binary_to_decimal (y )
115
+ c = a - b
116
+ d = decimal_to_binary (c )
117
+ return d
164
118
119
+ # Creating a StringVar to store the value of the entry field
165
120
e1_val = StringVar ()
121
+
122
+ # Creating the entry field
166
123
e1 = Entry (window , textvariable = e1_val , width = 50 )
167
124
e1 .grid (row = 0 , column = 0 , columnspan = 4 )
168
125
126
+ # Creating number buttons
169
127
b1 = Button (window , text = "1" , width = 8 , height = 2 , command = f2 )
170
128
b1 .grid (row = 1 , column = 0 )
171
129
172
130
b0 = Button (window , text = "0" , width = 8 , height = 2 , command = f3 )
173
131
b0 .grid (row = 1 , column = 1 )
174
132
133
+ # Creating clear button
175
134
clear = Button (window , text = "C" , width = 8 , height = 2 , command = f1 )
176
135
clear .grid (row = 1 , column = 2 )
177
136
137
+ # Creating equals button
178
138
beq = Button (window , text = "=" , width = 8 , height = 2 , command = f4 )
179
139
beq .grid (row = 1 , column = 3 )
180
140
141
+ # Creating operator buttons
181
142
badd = Button (window , text = "+" , width = 8 , height = 2 , command = f5 )
182
143
badd .grid (row = 2 , column = 0 )
183
144
@@ -190,4 +151,29 @@ def f8():
190
151
bdiv = Button (window , text = "/" , width = 8 , height = 2 , command = f7 )
191
152
bdiv .grid (row = 2 , column = 3 )
192
153
154
+ # Creating additional buttons
155
+ bnegate = Button (window , text = "+/-" , width = 8 , height = 2 , command = negate )
156
+ bnegate .grid (row = 3 , column = 0 )
157
+
158
+ bbackspace = Button (window , text = "⌫" , width = 8 , height = 2 , command = backspace )
159
+ bbackspace .grid (row = 3 , column = 1 )
160
+
161
+ bbinary = Button (window , text = "Bin to Dec" , width = 12 , height = 2 , command = on_binary )
162
+ bbinary .grid (row = 3 , column = 2 )
163
+
164
+ bdecimal = Button (window , text = "Dec to Bin" , width = 12 , height = 2 , command = on_decimal )
165
+ bdecimal .grid (row = 3 , column = 3 )
166
+
167
+ # Function to convert binary to decimal and update entry field
168
+ def on_binary ():
169
+ s = e1_val .get ()
170
+ decimal = binary_to_decimal (s )
171
+ e1_val .set (decimal_to_binary (decimal ))
172
+
173
+ # Function to convert decimal to binary and update entry field
174
+ def on_decimal ():
175
+ s = e1_val .get ()
176
+ binary = decimal_to_binary (int (s ))
177
+ e1_val .set (binary )
178
+
193
179
window .mainloop ()
0 commit comments