Skip to content

Commit abbee72

Browse files
authored
Implement Imperial Measurement Converter with Tkinter
This is a complete implementation of an Imperial Measurement Converter using Tkinter, allowing users to convert between various Imperial units.
1 parent e2fb9d2 commit abbee72

1 file changed

Lines changed: 185 additions & 0 deletions

File tree

ImperialMeasurementConverterApp.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# This still needs a bit of tidying up but it does actually work!
2+
# pip install tkinter
3+
import tkinter as tk
4+
from tkinter import *
5+
import tkinter.messagebox
6+
# import data_file as df
7+
8+
9+
class Unit:
10+
def __init__(self, name, base, B, In, F, Y, C, Fu, M):
11+
self.name = name
12+
self.base = base
13+
self.B = B
14+
self.In = In
15+
self.F = F
16+
self.Y = Y
17+
self.C = C
18+
self.Fu = Fu
19+
self.M = M
20+
21+
def get_base_value(self):
22+
return self.base
23+
24+
def get_B_value(self):
25+
return self.B
26+
27+
def get_inches_value(self):
28+
return self.In
29+
30+
def get_F_value(self):
31+
return self.F
32+
33+
def get_Y_value(self):
34+
return self.Y
35+
36+
def get_C_value(self):
37+
return self.C
38+
39+
def get_Fu_value(self):
40+
return self.Fu
41+
42+
def get_M_value(self):
43+
return self.M
44+
45+
46+
B = Unit("Barleycorn", 1, 1, 3, 36, 108, 2376, 23760, 190080)
47+
In = Unit("Inch", 3, 3, 1, 12, 36, 792, 7920, 63360)
48+
F = Unit("Foot", 36, 36, 12, 1, 3, 22, 220, 5280)
49+
Y = Unit("Yard", 108, 108, 36, 3, 1, 22, 220, 1760)
50+
C = Unit("Chain", 2376, 2376, 108, 66, 22, 1, 10, 80)
51+
Fu = Unit("Furlong", 23760, 23760, 63360, 660, 220, 10, 1, 8)
52+
M = Unit("Mile", 190080, 190080, 63360, 5280, 1760, 80, 8, 1)
53+
54+
55+
# Store units in a dictionary for easy access
56+
units = {
57+
"Barleycorn": B,
58+
"Inch": In,
59+
"Foot": F,
60+
"Yard": Y,
61+
"Chain": C,
62+
"Furlong": Fu,
63+
"Mile": M
64+
}
65+
66+
67+
root = tk.Tk()
68+
69+
root.title("Imperial Measurements Converter: Baines Services")
70+
71+
Tops = Frame(root, bg = '#e6e5e5', pady=2, width=1850, height=100, relief="ridge")
72+
Tops.grid(row=0, column=0)
73+
74+
headlabel = tk.Label(Tops, font=('lato black', 19, 'bold'), text='Imperial Measurements Converter: Baines Services',
75+
bg='#e6e5e5', fg='black')
76+
headlabel.grid(row=1, column=0, sticky=W)
77+
78+
variable1 = tk.StringVar(root)
79+
variable2 = tk.StringVar(root)
80+
81+
variable1.set("Input Measurement")
82+
variable2.set("Output Measurement")
83+
# Function To For Real Time Currency Conversion
84+
85+
86+
def RealTimeConverter():
87+
# Get user input for the unit
88+
selected_unit_name = variable1.get()
89+
selected_output_name = variable2.get()
90+
91+
# Check if the selected unit is valid
92+
if selected_unit_name in units:
93+
selected_unit = units[selected_unit_name]
94+
95+
# Get the amount to convert
96+
amount = float(Amount1_field.get())
97+
98+
# Calculate the result (amount * base value of the selected unit)
99+
result = amount * selected_unit.get_base_value()
100+
101+
# print(f"\nThe result for amount of barleycorns in {amount} of {selected_unit_name} is: {result}")
102+
103+
104+
# selected_output_name = input("\nSelect a unit from the list: ").capitalize()
105+
if selected_output_name in units:
106+
output_unit = units[selected_output_name]
107+
108+
if selected_output_name != 'Barleycorn':
109+
new_result = result / output_unit.get_B_value()
110+
elif selected_output_name != 'Inch':
111+
new_result = result / output_unit.get_inches_value()
112+
elif selected_output_name != "Foot":
113+
new_result = result / output_unit.get_F_value()
114+
elif selected_output_name != "Yard":
115+
new_result = result / output_unit.get_Y_value()
116+
elif selected_output_name != "Chain":
117+
new_result = result / output_unit.get_C_value()
118+
elif selected_output_name != "Furlong":
119+
new_result = result / output_unit.get_Fu_value()
120+
elif selected_output_name != "Mile":
121+
new_result = result / output_unit.get_M_value()
122+
123+
Amount2_field.insert(0, str(new_result))
124+
125+
else:
126+
print("Invalid unit selected. Please try again.")
127+
128+
129+
#clearing all the data entered by the user
130+
def clear_all():
131+
Amount1_field.delete(0, tk.END)
132+
Amount2_field.delete(0, tk.END)
133+
134+
135+
unit_list = ["Barleycorn", "Inch", "Foot", "Yard", "Chain", "Furlong", "Mile"]
136+
137+
root.configure(background='#e6e5e5')
138+
root.geometry("700x400")
139+
140+
Label_1 = Label(root, font=('lato black', 27, 'bold'), text="", padx=2, pady=2, bg="#e6e5e5", fg="black")
141+
Label_1.grid(row=1, column=0, sticky=W)
142+
143+
label1 = tk.Label(root, font=('lato black', 15, 'bold'), text="\t Amount : ", bg="#e6e5e5", fg="black")
144+
label1.grid(row=2, column=0, sticky=W)
145+
146+
label1 = tk.Label(root, font=('lato black', 15, 'bold'), text="\t From Currency : ", bg="#e6e5e5", fg="black")
147+
label1.grid(row=3, column=0, sticky=W)
148+
149+
label1 = tk.Label(root, font=('lato black', 15, 'bold'), text="\t To Currency : ", bg="#e6e5e5", fg="black")
150+
label1.grid(row=4, column=0, sticky=W)
151+
152+
label1 = tk.Label(root, font=('lato black', 15, 'bold'), text="\t Converted Amount : ", bg="#e6e5e5", fg="black")
153+
label1.grid(row=8, column=0, sticky=W)
154+
155+
Label_1 = Label(root, font=('lato black', 7, 'bold'), text="", padx=2, pady=2, bg="#e6e5e5", fg="black")
156+
Label_1.grid(row=5, column=0, sticky=W)
157+
158+
Label_1 = Label(root, font=('lato black', 7, 'bold'), text="", padx=2, pady=2, bg="#e6e5e5", fg="black")
159+
Label_1.grid(row=7, column=0, sticky=W)
160+
161+
selected_unit_name = tk.OptionMenu(root, variable1, *unit_list)
162+
selected_output_name = tk.OptionMenu(root, variable2, *unit_list)
163+
164+
selected_unit_name.grid(row=3, column=0, ipadx=45, sticky=E)
165+
selected_output_name.grid(row=4, column=0, ipadx=45, sticky=E)
166+
167+
Amount1_field = tk.Entry(root)
168+
Amount1_field.grid(row=2, column=0, ipadx=28, sticky=E)
169+
170+
Amount2_field = tk.Entry(root)
171+
Amount2_field.grid(row=8, column=0, ipadx=31, sticky=E)
172+
173+
Label_9 = Button(root, font=('arial', 15, 'bold'), text=" Convert ", padx=2, pady=2, bg="lightblue", fg="white",
174+
command=RealTimeConverter)
175+
Label_9.grid(row=6, column=0)
176+
177+
Label_1 = Label(root, font=('lato black', 7, 'bold'), text="", padx=2, pady=2, bg="#e6e5e5", fg="black")
178+
Label_1.grid(row=9, column=0, sticky=W)
179+
180+
Label_9 = Button(root, font=('arial', 15, 'bold'), text=" Clear All ", padx=2, pady=2, bg="lightblue", fg="white",
181+
command=clear_all)
182+
Label_9.grid(row=10, column=0)
183+
184+
185+
root.mainloop()

0 commit comments

Comments
 (0)