1
+ import tkinter as tk
2
+
3
+ def decimal_to_binary ():
4
+ try :
5
+ decimal_num = int (input_field .get ())
6
+ binary_num = bin (decimal_num ).replace ("0b" , "" )
7
+ output_label .config (text = f"Binary: { binary_num } " )
8
+ except ValueError :
9
+ output_label .config (text = "Invalid input. Please enter a decimal number." )
10
+
11
+ def binary_to_decimal ():
12
+ try :
13
+ binary_num = input_field .get ()
14
+ decimal_num = int (binary_num , 2 )
15
+ output_label .config (text = f"Decimal: { decimal_num } " )
16
+ except ValueError :
17
+ output_label .config (text = "Invalid input. Please enter a valid binary number." )
18
+
19
+ def clear_output ():
20
+ output_label .config (text = "" )
21
+
22
+ root = tk .Tk ()
23
+ root .title ("Binary-Decimal Converter" )
24
+
25
+ menu_frame = tk .Frame (root )
26
+ menu_frame .pack ()
27
+
28
+ decimal_to_binary_button = tk .Button (menu_frame , text = "Decimal to Binary" , command = decimal_to_binary )
29
+ decimal_to_binary_button .pack (side = tk .LEFT )
30
+
31
+ binary_to_decimal_button = tk .Button (menu_frame , text = "Binary to Decimal" , command = binary_to_decimal )
32
+ binary_to_decimal_button .pack (side = tk .LEFT )
33
+
34
+ clear_button = tk .Button (root , text = "Clear" , command = clear_output )
35
+ clear_button .pack ()
36
+
37
+ input_field = tk .Entry (root )
38
+ input_field .pack ()
39
+
40
+ convert_button = tk .Button (root , text = "Convert" , command = lambda : None ) # Placeholder for now
41
+ convert_button .pack ()
42
+
43
+ output_label = tk .Label (root , text = "" , pady = 10 )
44
+ output_label .pack ()
45
+
46
+ root .mainloop ()
0 commit comments