|
1 |
| -conversion_table = {0: '0', 1: '1', 2: '2', 3: '3', 4: '4', |
2 |
| - 5: '5', 6: '6', 7: '7', |
3 |
| - 8: '8', 9: '9', 10: 'A', 11: 'B', 12: 'C', |
4 |
| - 13: 'D', 14: 'E', 15: 'F'} |
| 1 | +import tkinter as tk |
5 | 2 |
|
| 3 | +def hex_add(): |
| 4 | + try: |
| 5 | + num1 = int(entry1.get(), 16) |
| 6 | + num2 = int(entry2.get(), 16) |
| 7 | + result = hex(num1 + num2) |
| 8 | + output.config(text="Result: " + result.upper()) |
| 9 | + except ValueError: |
| 10 | + output.config(text="Invalid Input") |
6 | 11 |
|
7 |
| -def decimalToHexadecimal(a): |
8 |
| - b = '' |
9 |
| - while (a > 0): |
10 |
| - remainder = a % 16 |
11 |
| - b = conversion_table[remainder] + b |
12 |
| - a = a // 16 |
| 12 | +def hex_subtract(): |
| 13 | + try: |
| 14 | + num1 = int(entry1.get(), 16) |
| 15 | + num2 = int(entry2.get(), 16) |
| 16 | + result = hex(num1 - num2) |
| 17 | + output.config(text="Result: " + result.upper()) |
| 18 | + except ValueError: |
| 19 | + output.config(text="Invalid Input") |
13 | 20 |
|
14 |
| - return b |
| 21 | +def hex_multiply(): |
| 22 | + try: |
| 23 | + num1 = int(entry1.get(), 16) |
| 24 | + num2 = int(entry2.get(), 16) |
| 25 | + result = hex(num1 * num2) |
| 26 | + output.config(text="Result: " + result.upper()) |
| 27 | + except ValueError: |
| 28 | + output.config(text="Invalid Input") |
15 | 29 |
|
| 30 | +def hex_divide(): |
| 31 | + try: |
| 32 | + num1 = int(entry1.get(), 16) |
| 33 | + num2 = int(entry2.get(), 16) |
| 34 | + result = hex(num1 // num2) |
| 35 | + output.config(text="Result: " + result.upper()) |
| 36 | + except (ValueError, ZeroDivisionError): |
| 37 | + output.config(text="Invalid Input") |
16 | 38 |
|
17 |
| -decimal = int(input("Enter decimal value: ")) |
18 |
| -hexadecimal = decimalToHexadecimal(decimal) |
| 39 | +# Main tkinter window |
| 40 | +root = tk.Tk() |
| 41 | +root.title("Hex Calculator") |
| 42 | + |
| 43 | +# Entry fields |
| 44 | +entry1 = tk.Entry(root, width=15) |
| 45 | +entry1.grid(row=0, column=0, padx=10, pady=5) |
| 46 | +entry2 = tk.Entry(root, width=15) |
| 47 | +entry2.grid(row=0, column=1, padx=10, pady=5) |
| 48 | + |
| 49 | +# Buttons |
| 50 | +add_button = tk.Button(root, text="Add", command=hex_add) |
| 51 | +add_button.grid(row=1, column=0, padx=10, pady=5) |
| 52 | +subtract_button = tk.Button(root, text="Subtract", command=hex_subtract) |
| 53 | +subtract_button.grid(row=1, column=1, padx=10, pady=5) |
| 54 | +multiply_button = tk.Button(root, text="Multiply", command=hex_multiply) |
| 55 | +multiply_button.grid(row=2, column=0, padx=10, pady=5) |
| 56 | +divide_button = tk.Button(root, text="Divide", command=hex_divide) |
| 57 | +divide_button.grid(row=2, column=1, padx=10, pady=5) |
| 58 | + |
| 59 | +# Output Label |
| 60 | +output = tk.Label(root, text="Result: ") |
| 61 | +output.grid(row=3, columnspan=2) |
| 62 | + |
| 63 | +root.mainloop() |
19 | 64 |
|
20 |
| -print("Decimal Value:", decimal) |
21 |
| -print("Hexadecimal Value:", hexadecimal) |
|
0 commit comments