Amazing-Python-Scripts
217 строк · 4.5 Кб
1from tkinter import *2
3window = Tk()4window.title("Standard Binary Calculator")5window.resizable(0, 0)6
7# Function to clear the entry field
8
9
10def f1():11s = e1_val.get()12e1.delete(first=0, last=len(s))13
14# Function to add "1" to the entry field
15
16
17def f2():18s = e1_val.get()19e1.insert(END, "1")20
21# Function to add "0" to the entry field
22
23
24def f3():25s = e1_val.get()26e1.insert(END, "0")27
28# Function to evaluate the expression and display the result
29
30
31def f4():32s = e1_val.get()33result = evaluate_expression(s)34e1.delete(first=0, last=len(s))35e1.insert(END, result)36
37# Function to add "+" operator to the entry field
38
39
40def f5():41e1.insert(END, "+")42
43# Function to add "-" operator to the entry field
44
45
46def f6():47e1.insert(END, "-")48
49# Function to add "/" operator to the entry field
50
51
52def f7():53e1.insert(END, "/")54
55# Function to add "X" operator to the entry field
56
57
58def f8():59e1.insert(END, "X")60
61# Function to negate the sign of the number in the entry field
62
63
64def negate():65s = e1_val.get()66if s.startswith("-"):67e1_val.set(s[1:])68else:69e1_val.set("-" + s)70
71# Function to delete the last character in the entry field
72
73
74def backspace():75s = e1_val.get()76e1_val.set(s[:-1])77
78# Function to convert binary to decimal
79
80
81def binary_to_decimal(n):82num = n83dec_value = 084base = 185temp = num86while temp:87last_digit = temp % 1088temp = temp // 1089dec_value += last_digit * base90base = base * 291return dec_value92
93# Function to convert decimal to binary
94
95
96def decimal_to_binary(n):97if n == 0:98return '0'99binary = ''100while n > 0:101binary = str(n % 2) + binary102n = n // 2103return binary104
105# Function to evaluate the expression
106
107
108def evaluate_expression(expression):109x = 0110s = expression111flag = 1112for i in range(0, len(s)):113if s[i] == '/' or s[i] == 'X' or s[i] == '+' or s[i] == '-':114flag = 0115a = s[0:i]116b = s[i + 1:len(s)]117if s[i] == '-':118x = sub(int(a), int(b))119elif s[i] == '/':120x = int(int(a) / int(b))121elif s[i] == 'X':122x = int(int(a) * int(b))123elif s[i] == '+':124x = int(add(int(a), int(b)))125if flag == 0:126return str(x)127return ""128
129# Function to perform binary addition
130
131
132def add(x, y):133a = binary_to_decimal(x)134b = binary_to_decimal(y)135c = a + b136d = decimal_to_binary(c)137return d138
139# Function to perform binary subtraction
140
141
142def sub(x, y):143a = binary_to_decimal(x)144b = binary_to_decimal(y)145c = a - b146d = decimal_to_binary(c)147return d148
149
150# Creating a StringVar to store the value of the entry field
151e1_val = StringVar()152
153# Creating the entry field
154e1 = Entry(window, textvariable=e1_val, width=50)155e1.grid(row=0, column=0, columnspan=4)156
157# Creating number buttons
158b1 = Button(window, text="1", width=8, height=2, command=f2)159b1.grid(row=1, column=0)160
161b0 = Button(window, text="0", width=8, height=2, command=f3)162b0.grid(row=1, column=1)163
164# Creating clear button
165clear = Button(window, text="C", width=8, height=2, command=f1)166clear.grid(row=1, column=2)167
168# Creating equals button
169beq = Button(window, text="=", width=8, height=2, command=f4)170beq.grid(row=1, column=3)171
172# Creating operator buttons
173badd = Button(window, text="+", width=8, height=2, command=f5)174badd.grid(row=2, column=0)175
176bsub = Button(window, text="-", width=8, height=2, command=f6)177bsub.grid(row=2, column=1)178
179bmul = Button(window, text="X", width=8, height=2, command=f8)180bmul.grid(row=2, column=2)181
182bdiv = Button(window, text="/", width=8, height=2, command=f7)183bdiv.grid(row=2, column=3)184
185# Creating additional buttons
186bnegate = Button(window, text="+/-", width=8, height=2, command=negate)187bnegate.grid(row=3, column=0)188
189bbackspace = Button(window, text="⌫", width=8, height=2, command=backspace)190bbackspace.grid(row=3, column=1)191
192bbinary = Button(window, text="Bin to Dec", width=12,193height=2, command=on_binary)194bbinary.grid(row=3, column=2)195
196bdecimal = Button(window, text="Dec to Bin", width=12,197height=2, command=on_decimal)198bdecimal.grid(row=3, column=3)199
200# Function to convert binary to decimal and update entry field
201
202
203def on_binary():204s = e1_val.get()205decimal = binary_to_decimal(s)206e1_val.set(decimal_to_binary(decimal))207
208# Function to convert decimal to binary and update entry field
209
210
211def on_decimal():212s = e1_val.get()213binary = decimal_to_binary(int(s))214e1_val.set(binary)215
216
217window.mainloop()218