-
Notifications
You must be signed in to change notification settings - Fork 1
/
B to D & D to B converter.py
46 lines (34 loc) · 1.41 KB
/
B to D & D to B converter.py
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
import tkinter as tk
def decimal_to_binary():
try:
decimal_num = int(input_field.get())
binary_num = bin(decimal_num).replace("0b", "")
output_label.config(text=f"Binary: {binary_num}")
except ValueError:
output_label.config(text="Invalid input. Please enter a decimal number.")
def binary_to_decimal():
try:
binary_num = input_field.get()
decimal_num = int(binary_num, 2)
output_label.config(text=f"Decimal: {decimal_num}")
except ValueError:
output_label.config(text="Invalid input. Please enter a valid binary number.")
def clear_output():
output_label.config(text="")
root = tk.Tk()
root.title("Binary-Decimal Converter")
menu_frame = tk.Frame(root)
menu_frame.pack()
decimal_to_binary_button = tk.Button(menu_frame, text="Decimal to Binary", command=decimal_to_binary)
decimal_to_binary_button.pack(side=tk.LEFT)
binary_to_decimal_button = tk.Button(menu_frame, text="Binary to Decimal", command=binary_to_decimal)
binary_to_decimal_button.pack(side=tk.LEFT)
clear_button = tk.Button(root, text="Clear", command=clear_output)
clear_button.pack()
input_field = tk.Entry(root)
input_field.pack()
convert_button = tk.Button(root, text="Convert", command=lambda: None) # Placeholder for now
convert_button.pack()
output_label = tk.Label(root, text="", pady=10)
output_label.pack()
root.mainloop()