-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
292 lines (220 loc) · 10.7 KB
/
Copy pathmain.py
File metadata and controls
292 lines (220 loc) · 10.7 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from customtkinter import *
from CTkMessagebox import CTkMessagebox
# from PIL import Image
from logics import *
class Interface(CTk):
BINARY_OPERATIONS_SCREEN_TITLES = {
'1': "Division",
'2': "Multiplication",
'3': "Subtraction",
'4': "Addition",
'5': "Negative (Two's Complement)",
}
BINARY_OPERATIONS_SCREEN_OPTIONS = {
'1': BinaryDivision(),
'2': BinaryMultiplication(),
'3': BinarySubtraction(),
'4': BinaryAddition(),
'5': BinaryNegative(),
}
NUMBER_SYSTEM_CONVERSION_SCREEN_TITLES = {
'1': "Binary to X",
'2': "Decimal to X",
'3': "Octal to X",
'4': "Hex to X",
}
NUMBER_SYSTEM_CONVERSION_OPTIONS = {
'1': FromBinary(),
'2': FromDecimal(),
'3': FromOctal(),
'4': FromHex(),
}
def __init__(self):
super().__init__()
self.title('ByteMe')
self.geometry('550x500')
self.minsize(500, 500)
# Modes: "System" (standard), "Dark", "Light"
set_appearance_mode("Dark")
if get_appearance_mode() == "Dark":
set_default_color_theme("assets/themes/pink.json")
else:
set_default_color_theme("assets/themes/carrot.json")
set_widget_scaling(1.5)
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
self.main_menu()
# 1st Screen Display the Main Menu
def main_menu(self):
self.clear_screen()
menu_frame = self.create_frame()
menu_label = CTkLabel(menu_frame, text="Main Menu",
font=('Helvetica', 20, "bold"))
menu_label.pack(padx=10, pady=10)
binary_button = CTkButton(
menu_frame, text="Binary Operations", command=self.binary_operations_menu)
binary_button.pack(padx=5, pady=5)
conversion_button = CTkButton(
menu_frame, text="Number System Conversion", command=self.number_system_conversion_menu)
conversion_button.pack(pady=5)
exit_button = CTkButton(menu_frame, text="Exit",
command=self.exit_screen)
exit_button.pack(pady=5)
# 2nd Screen Display the Binary Operations Menu
def binary_operations_menu(self):
self.clear_screen()
menu_frame = self.create_frame()
menu_label = CTkLabel(
menu_frame, text="Binary Operations Menu", font=('Helvetica', 20, "bold"))
menu_label.pack(pady=10)
division_button = CTkButton(menu_frame, text="Division",
command=lambda: self.perform_binary_operation_input_screen('1'))
division_button.pack(padx=5, pady=5)
multiplication_button = CTkButton(menu_frame, text="Multiplication",
command=lambda: self.perform_binary_operation_input_screen('2'))
multiplication_button.pack(padx=5, pady=5)
subtraction_button = CTkButton(menu_frame, text="Subtraction",
command=lambda: self.perform_binary_operation_input_screen('3'))
subtraction_button.pack(padx=5, pady=5)
addition_button = CTkButton(menu_frame, text="Addition",
command=lambda: self.perform_binary_operation_input_screen('4'))
addition_button.pack(padx=5, pady=5)
twos_complement_button = CTkButton(menu_frame, text="Negative (Two's Complement)",
command=lambda: self.perform_binary_operation_input_screen('5'))
twos_complement_button.pack(padx=5, pady=5)
back_button = CTkButton(
menu_frame, text="Back to Main Menu", command=self.main_menu)
back_button.pack(padx=5, pady=5)
# Display the Input Screen of Binary Operation
def perform_binary_operation_input_screen(self, choice):
self.clear_screen()
title = self.BINARY_OPERATIONS_SCREEN_TITLES[choice]
menu_frame = self.create_frame()
menu_label = CTkLabel(menu_frame, text=title,
font=('Helvetica', 20, "bold"))
menu_label.pack(padx=10, pady=10)
binary_num1_label = CTkLabel(
menu_frame, text="Enter the first binary number:")
binary_num1_label.pack()
binary_num1_entry = CTkEntry(
menu_frame, placeholder_text="e.g. 0100 1010")
binary_num1_entry.pack()
if choice != '5':
binary_num2_label = CTkLabel(
menu_frame, text="Enter the second binary number:")
binary_num2_label.pack()
binary_num2_entry = CTkEntry(
menu_frame, placeholder_text="e.g. 1111 0001")
binary_num2_entry.pack()
# Button to perform the operation
if choice != '5':
perform_button = CTkButton(menu_frame, text="Perform Operation",
command=lambda:
self.perform_binary_operation_output_screen(choice=choice, binary_num1=binary_num1_entry.get() if binary_num1_entry.get() != '' else '0', binary_num2=binary_num2_entry.get() if binary_num2_entry.get() != '' else '0'))
else:
perform_button = CTkButton(menu_frame, text="Perform Operation",
command=lambda:
self.perform_binary_operation_output_screen(choice=choice, binary_num1=binary_num1_entry.get() if binary_num1_entry.get() != '' else '0'))
perform_button.pack(pady=10)
# Display the Output Screen of Binary Operation
def perform_binary_operation_output_screen(self,
choice: str,
binary_num1: str,
binary_num2: str | None = None):
self.clear_screen()
title = self.BINARY_OPERATIONS_SCREEN_TITLES[choice]
menu_frame = self.create_frame()
menu_label = CTkLabel(menu_frame, text=title,
font=('Helvetica', 20, "bold"))
menu_label.pack(pady=10)
operation = self.BINARY_OPERATIONS_SCREEN_OPTIONS[choice]
result = ''
if choice == '5':
result = operation.perform(binary_num1)
else:
result = operation.perform(binary_num1, binary_num2)
# Create Labels to display the results
result_label = CTkLabel(
menu_frame, text=f"{operation.get_name()} result: {result}")
result_label.pack(pady=10)
# Button to return to the conversion menu
back_button = CTkButton(
menu_frame, text="Back to Binary Operations Menu", command=self.binary_operations_menu)
back_button.pack(pady=5)
# 3rd Screen Display the Number Conversion System
def number_system_conversion_menu(self):
self.clear_screen()
menu_frame = self.create_frame()
menu_label = CTkLabel(
menu_frame, text="Number System Conversion Menu", font=('Helvetica', 20, "bold"))
menu_label.pack(pady=10)
binary_to_x_button = CTkButton(menu_frame, text="Binary to X",
command=lambda: self.number_system_conversion_input_screen('1'))
binary_to_x_button.pack(padx=5, pady=5)
decimal_to_x_button = CTkButton(menu_frame, text="Decimal to X",
command=lambda: self.number_system_conversion_input_screen('2'))
decimal_to_x_button.pack(padx=5, pady=5)
octal_to_x_button = CTkButton(menu_frame, text="Octal to X",
command=lambda: self.number_system_conversion_input_screen('3'))
octal_to_x_button.pack(padx=5, pady=5)
hex_to_x_button = CTkButton(menu_frame, text="Hex to X",
command=lambda: self.number_system_conversion_input_screen('4'))
hex_to_x_button.pack(padx=5, pady=5)
back_button = CTkButton(
menu_frame, text="Back to Main Menu", command=self.main_menu)
back_button.pack(padx=5, pady=5)
def number_system_conversion_input_screen(self, base):
self.clear_screen()
title = self.NUMBER_SYSTEM_CONVERSION_SCREEN_TITLES[base]
menu_frame = self.create_frame()
menu_label = CTkLabel(menu_frame, text=title,
font=('Helvetica', 20, "bold"))
menu_label.pack(pady=10)
number_label = CTkLabel(menu_frame, text="Enter number to convert:")
number_label.pack(pady=5)
number_entry = CTkEntry(menu_frame)
number_entry.pack(pady=5)
perform_button = CTkButton(menu_frame, text="Convert",
command=lambda: self.number_system_conversion_output_screen(base=base, number=number_entry.get() if number_entry.get() != '' else '0'))
perform_button.pack(pady=5)
def number_system_conversion_output_screen(self, base: str, number: str):
self.clear_screen()
title = self.NUMBER_SYSTEM_CONVERSION_SCREEN_TITLES[base]
menu_frame = self.create_frame()
menu_label = CTkLabel(menu_frame, text=title,
font=('Helvetica', 20, "bold"))
menu_label.pack(pady=10)
conversion = self.NUMBER_SYSTEM_CONVERSION_OPTIONS[base]
binary_result = conversion.to_binary(number)
decimal_result = conversion.to_decimal(number)
octal_result = conversion.to_octal(number)
hex_result = conversion.to_hex(number)
# Create Labels to display the results
binary_label = CTkLabel(menu_frame, text=f'Binary: {binary_result}')
binary_label.pack()
decimal_label = CTkLabel(menu_frame, text=f'Decimal: {decimal_result}')
decimal_label.pack()
octal_label = CTkLabel(menu_frame, text=f'Octal: {octal_result}')
octal_label.pack()
hex_label = CTkLabel(menu_frame, text=f'Hex: {hex_result}')
hex_label.pack()
# Button to return to the conversion menu
back_button = CTkButton(
menu_frame, text="Back to Conversion Menu", command=self.number_system_conversion_menu)
back_button.pack(pady=5)
def exit_screen(self):
msg = CTkMessagebox(title="ByteMe | Quit", message="Do you want to quit?",
option_1="No", option_2="Yes", corner_radius=0, sound=True, option_focus=1)
response = msg.get()
if response == "Yes":
self.destroy()
def clear_screen(self):
for widget in self.winfo_children():
widget.destroy()
def create_frame(self):
menu_frame = CTkFrame(self, corner_radius=0)
menu_frame.grid(row=0, column=0, sticky='nsew')
return menu_frame
if __name__ == '__main__':
app = Interface()
app.mainloop()