-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
55 lines (43 loc) · 1.49 KB
/
convert.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
47
48
49
50
51
52
53
54
55
import tkinter as tk
from tkinter import ttk
def convert():
try:
input_value = float(entry.get())
from_unit = from_combobox.get()
to_unit = to_combobox.get()
conversion_factors = {
'Kilometers': 1000,
'Meters': 1,
'Centimeters': 0.01,
'Millimeters': 0.001,
}
result = input_value * conversion_factors[from_unit] / conversion_factors[to_unit]
result_label.config(text=f"Result: {result:.2f} {to_unit}")
except ValueError:
result_label.config(text="Invalid input! Please enter a number.")
app = tk.Tk()
app.title("Unit Converter")
app.geometry("300x250")
entry_label = tk.Label(app, text="Enter value:")
entry_label.pack(pady=5)
entry = tk.Entry(app, width=20)
entry.pack(pady=5)
from_label = tk.Label(app, text="From:")
from_label.pack(pady=5)
from_combobox = ttk.Combobox(app, values=["Kilometers", "Meters", "Centimeters", "Millimeters"])
from_combobox.pack(pady=5)
from_combobox.current(1)
# To unit combobox
to_label = tk.Label(app, text="To:")
to_label.pack(pady=5)
to_combobox = ttk.Combobox(app, values=["Kilometers", "Meters", "Centimeters", "Millimeters"])
to_combobox.pack(pady=5)
to_combobox.current(2)
# Convert button
convert_button = tk.Button(app, text="Convert", command=convert)
convert_button.pack(pady=10)
# Result label
result_label = tk.Label(app, text="Result: ", font=("Arial", 12))
result_label.pack(pady=10)
# Run the application
app.mainloop()