Skip to content

Commit 9bde615

Browse files
authored
Update script.py
his code includes the previous functions, f1, f2, f3, f4, f5, f6, f7, f8, negate, and backspace, as well as the additional functions binary_to_decimal and decimal_to_binary. The buttons bbinary and bdecimal allow you to convert between binary and decimal representations. I've added comments throughout the code to explain the purpose and functionality of each section. This should help understand the code more easily.
1 parent fd21822 commit 9bde615

File tree

1 file changed

+100
-114
lines changed

1 file changed

+100
-114
lines changed

Binary-Calculator-GUI/script.py

Lines changed: 100 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,87 @@
11
from tkinter import *
2+
23
window = Tk()
34
window.title("Standard Binary Calculator")
45
window.resizable(0, 0)
56

6-
7+
# Function to clear the entry field
78
def f1():
89
s = e1_val.get()
910
e1.delete(first=0, last=len(s))
1011

11-
12+
# Function to add "1" to the entry field
1213
def f2():
1314
s = e1_val.get()
1415
e1.insert(END, "1")
1516

16-
17+
# Function to add "0" to the entry field
1718
def f3():
1819
s = e1_val.get()
1920
e1.insert(END, "0")
2021

21-
22+
# Function to evaluate the expression and display the result
2223
def f4():
23-
x = 0
2424
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)
3826
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, "/")
4140

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)
4252

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):
4460
num = n
4561
dec_value = 0
4662
base = 1
4763
temp = num
48-
while (temp):
64+
while temp:
4965
last_digit = temp % 10
50-
temp = int(temp / 10)
51-
66+
temp = temp // 10
5267
dec_value += last_digit * base
5368
base = base * 2
5469
return dec_value
5570

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):
12083
x = 0
121-
s = e1_val.get()
84+
s = expression
12285
flag = 1
12386
for i in range(0, len(s)):
12487
if s[i] == '/' or s[i] == 'X' or s[i] == '+' or s[i] == '-':
@@ -134,50 +97,48 @@ def f7():
13497
elif s[i] == '+':
13598
x = int(add(int(a), int(b)))
13699
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 ""
140102

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
141110

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
164118

119+
# Creating a StringVar to store the value of the entry field
165120
e1_val = StringVar()
121+
122+
# Creating the entry field
166123
e1 = Entry(window, textvariable=e1_val, width=50)
167124
e1.grid(row=0, column=0, columnspan=4)
168125

126+
# Creating number buttons
169127
b1 = Button(window, text="1", width=8, height=2, command=f2)
170128
b1.grid(row=1, column=0)
171129

172130
b0 = Button(window, text="0", width=8, height=2, command=f3)
173131
b0.grid(row=1, column=1)
174132

133+
# Creating clear button
175134
clear = Button(window, text="C", width=8, height=2, command=f1)
176135
clear.grid(row=1, column=2)
177136

137+
# Creating equals button
178138
beq = Button(window, text="=", width=8, height=2, command=f4)
179139
beq.grid(row=1, column=3)
180140

141+
# Creating operator buttons
181142
badd = Button(window, text="+", width=8, height=2, command=f5)
182143
badd.grid(row=2, column=0)
183144

@@ -190,4 +151,29 @@ def f8():
190151
bdiv = Button(window, text="/", width=8, height=2, command=f7)
191152
bdiv.grid(row=2, column=3)
192153

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+
193179
window.mainloop()

0 commit comments

Comments
 (0)