Skip to content

Commit 2cffc8f

Browse files
committed
Improved the calculator by using tkinter
1 parent 0b9e78c commit 2cffc8f

File tree

2 files changed

+66
-28
lines changed

2 files changed

+66
-28
lines changed

hexCalculator/README.md

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
The aim of a decimal to hexadecimal (hex) calculator is to provide a tool that allows users to convert decimal numbers into their equivalent hexadecimal representation.
44

5-
Decimal to hexadecimal calculator takes a decimal number as input and converts it into a hexadecimal number.
6-
The following steps are involved in the conversion process:
7-
- User input
8-
- Validation
9-
- Conversion algorithm
10-
- Hexadecimal Output
5+
The code above creates a simple hex calculator using the Python tkinter library. The calculator allows users to add, subtract, multiply, and divide two hexadecimal numbers. The input fields are two text boxes where users can enter the hexadecimal numbers. The buttons at the bottom of the window perform the four operations. The output label displays the result of the operation.
116

127
## Language
13-
- [x] Python
8+
- Python
9+
- Tkinter
1410

1511
## Setup instructions
1612
Run the below command to show output
@@ -20,12 +16,11 @@ python hexCalculator.py
2016

2117
## Output
2218
```
23-
Enter decimal value: 10
24-
Decimal Value: 10
25-
Hexadecimal Value: A
19+
Here is the output of the code when the user enters 0x12 in the first text box, 0x34 in the second text box, and then clicks the Add button:
20+
Result: 0x46
2621
```
2722
```
28-
Enter decimal value: 50
29-
Decimal Value: 50
30-
Hexadecimal Value: 32
23+
If the user enters 0x12 in the first text box and 0x34 in the second text box, and then clicks the Add button.
24+
output: 0x46.
25+
This is the hexadecimal representation of the decimal number 56.
3126
```

hexCalculator/hexCalculator.py

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,64 @@
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
52

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")
611

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")
1320

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")
1529

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")
1638

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()
1964

20-
print("Decimal Value:", decimal)
21-
print("Hexadecimal Value:", hexadecimal)

0 commit comments

Comments
 (0)