Amazing-Python-Scripts

Форк
0
217 строк · 4.5 Кб
1
from tkinter import *
2

3
window = Tk()
4
window.title("Standard Binary Calculator")
5
window.resizable(0, 0)
6

7
# Function to clear the entry field
8

9

10
def f1():
11
    s = e1_val.get()
12
    e1.delete(first=0, last=len(s))
13

14
# Function to add "1" to the entry field
15

16

17
def f2():
18
    s = e1_val.get()
19
    e1.insert(END, "1")
20

21
# Function to add "0" to the entry field
22

23

24
def f3():
25
    s = e1_val.get()
26
    e1.insert(END, "0")
27

28
# Function to evaluate the expression and display the result
29

30

31
def f4():
32
    s = e1_val.get()
33
    result = evaluate_expression(s)
34
    e1.delete(first=0, last=len(s))
35
    e1.insert(END, result)
36

37
# Function to add "+" operator to the entry field
38

39

40
def f5():
41
    e1.insert(END, "+")
42

43
# Function to add "-" operator to the entry field
44

45

46
def f6():
47
    e1.insert(END, "-")
48

49
# Function to add "/" operator to the entry field
50

51

52
def f7():
53
    e1.insert(END, "/")
54

55
# Function to add "X" operator to the entry field
56

57

58
def f8():
59
    e1.insert(END, "X")
60

61
# Function to negate the sign of the number in the entry field
62

63

64
def negate():
65
    s = e1_val.get()
66
    if s.startswith("-"):
67
        e1_val.set(s[1:])
68
    else:
69
        e1_val.set("-" + s)
70

71
# Function to delete the last character in the entry field
72

73

74
def backspace():
75
    s = e1_val.get()
76
    e1_val.set(s[:-1])
77

78
# Function to convert binary to decimal
79

80

81
def binary_to_decimal(n):
82
    num = n
83
    dec_value = 0
84
    base = 1
85
    temp = num
86
    while temp:
87
        last_digit = temp % 10
88
        temp = temp // 10
89
        dec_value += last_digit * base
90
        base = base * 2
91
    return dec_value
92

93
# Function to convert decimal to binary
94

95

96
def decimal_to_binary(n):
97
    if n == 0:
98
        return '0'
99
    binary = ''
100
    while n > 0:
101
        binary = str(n % 2) + binary
102
        n = n // 2
103
    return binary
104

105
# Function to evaluate the expression
106

107

108
def evaluate_expression(expression):
109
    x = 0
110
    s = expression
111
    flag = 1
112
    for i in range(0, len(s)):
113
        if s[i] == '/' or s[i] == 'X' or s[i] == '+' or s[i] == '-':
114
            flag = 0
115
            a = s[0:i]
116
            b = s[i + 1:len(s)]
117
            if s[i] == '-':
118
                x = sub(int(a), int(b))
119
            elif s[i] == '/':
120
                x = int(int(a) / int(b))
121
            elif s[i] == 'X':
122
                x = int(int(a) * int(b))
123
            elif s[i] == '+':
124
                x = int(add(int(a), int(b)))
125
    if flag == 0:
126
        return str(x)
127
    return ""
128

129
# Function to perform binary addition
130

131

132
def add(x, y):
133
    a = binary_to_decimal(x)
134
    b = binary_to_decimal(y)
135
    c = a + b
136
    d = decimal_to_binary(c)
137
    return d
138

139
# Function to perform binary subtraction
140

141

142
def sub(x, y):
143
    a = binary_to_decimal(x)
144
    b = binary_to_decimal(y)
145
    c = a - b
146
    d = decimal_to_binary(c)
147
    return d
148

149

150
# Creating a StringVar to store the value of the entry field
151
e1_val = StringVar()
152

153
# Creating the entry field
154
e1 = Entry(window, textvariable=e1_val, width=50)
155
e1.grid(row=0, column=0, columnspan=4)
156

157
# Creating number buttons
158
b1 = Button(window, text="1", width=8, height=2, command=f2)
159
b1.grid(row=1, column=0)
160

161
b0 = Button(window, text="0", width=8, height=2, command=f3)
162
b0.grid(row=1, column=1)
163

164
# Creating clear button
165
clear = Button(window, text="C", width=8, height=2, command=f1)
166
clear.grid(row=1, column=2)
167

168
# Creating equals button
169
beq = Button(window, text="=", width=8, height=2, command=f4)
170
beq.grid(row=1, column=3)
171

172
# Creating operator buttons
173
badd = Button(window, text="+", width=8, height=2, command=f5)
174
badd.grid(row=2, column=0)
175

176
bsub = Button(window, text="-", width=8, height=2, command=f6)
177
bsub.grid(row=2, column=1)
178

179
bmul = Button(window, text="X", width=8, height=2, command=f8)
180
bmul.grid(row=2, column=2)
181

182
bdiv = Button(window, text="/", width=8, height=2, command=f7)
183
bdiv.grid(row=2, column=3)
184

185
# Creating additional buttons
186
bnegate = Button(window, text="+/-", width=8, height=2, command=negate)
187
bnegate.grid(row=3, column=0)
188

189
bbackspace = Button(window, text="⌫", width=8, height=2, command=backspace)
190
bbackspace.grid(row=3, column=1)
191

192
bbinary = Button(window, text="Bin to Dec", width=12,
193
                 height=2, command=on_binary)
194
bbinary.grid(row=3, column=2)
195

196
bdecimal = Button(window, text="Dec to Bin", width=12,
197
                  height=2, command=on_decimal)
198
bdecimal.grid(row=3, column=3)
199

200
# Function to convert binary to decimal and update entry field
201

202

203
def on_binary():
204
    s = e1_val.get()
205
    decimal = binary_to_decimal(s)
206
    e1_val.set(decimal_to_binary(decimal))
207

208
# Function to convert decimal to binary and update entry field
209

210

211
def on_decimal():
212
    s = e1_val.get()
213
    binary = decimal_to_binary(int(s))
214
    e1_val.set(binary)
215

216

217
window.mainloop()
218

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.