Skip to content

Commit 096ac16

Browse files
Merge pull request avinashkranjan#832 from tanvi355/master
Distance conversion GUI
2 parents b58c72c + 9d5ee6f commit 096ac16

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

Distance Conversion GUI/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# DISTANCE UNIT CONVERTER or LENGTH UNIT CONVERTER
2+
3+
## Description
4+
This is a distance unit converter using which we can convert the distance from one unit to other.
5+
6+
Ex. cm to miles, feet to inches, etc.
7+
8+
## Module used
9+
- Tkinter
10+
11+
If you don't have Tkinter installed on your machine you can write:
12+
13+
- In command prompt
14+
```
15+
pip install tk
16+
```
17+
- In conda prompt
18+
```
19+
conda install -c anaconda tk
20+
```
21+
22+
## Output
23+
<p align="center">
24+
<img src="https://github.com/tanvi355/Amazing-Python-Scripts/blob/master/Distance%20Conversion%20GUI/dist_conv1.PNG">
25+
<br>
26+
Initial window
27+
<br><br>
28+
<img src="https://github.com/tanvi355/Amazing-Python-Scripts/blob/master/Distance%20Conversion%20GUI/dist_conv2.PNG">
29+
<br>
30+
Conversion performed
31+
<br><br>
32+
<img src="https://github.com/tanvi355/Amazing-Python-Scripts/blob/master/Distance%20Conversion%20GUI/dist_conv3.PNG">
33+
<br>
34+
Warning message
35+
<br><br>
36+
</p>
37+
38+
## Author
39+
[Tanvi Bugdani](https://github.com/tanvi355)
40+
20.6 KB
Loading
26.1 KB
Loading
29.4 KB
Loading
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import tkinter as tk
2+
from tkinter import *
3+
from tkinter import messagebox
4+
5+
#main window
6+
root = tk.Tk()
7+
#window size (width x height)
8+
root.geometry("600x350")
9+
#title of window
10+
root.title("Distance Unit Converter")
11+
#disabling resizing of window
12+
root.resizable(0, 0)
13+
#background colour of window
14+
root['bg'] = 'skyblue'
15+
16+
#conversion factors
17+
conv_factors = {
18+
"cm" : 0.01,
19+
"m" : 1.0,
20+
"km": 1000.0,
21+
"feet": 0.3048,
22+
"miles": 1609.344,
23+
"inches": 0.0254,
24+
"yards": 0.9144
25+
}
26+
27+
#function to convert from one unit to other
28+
def convert():
29+
ip = float(input.get())
30+
ip_unit = ip_opt.get()
31+
op_unit = op_opt.get()
32+
#if user chooses same input & output unit then display warning
33+
if(ip_unit==op_unit):
34+
messagebox.showwarning("Warning","Select different units")
35+
#else perform conversion
36+
else:
37+
output.delete(0, END)
38+
ans = ip*(conv_factors[ip_unit]/conv_factors[op_unit])
39+
output.insert(0, round(ans, 4))
40+
41+
#menu default text
42+
ip_opt = StringVar()
43+
ip_opt.set("Select Unit")
44+
op_opt = StringVar()
45+
op_opt.set("Select Unit")
46+
47+
#Adding Widgets
48+
greeting = Label(text="Welcome to Distance Unit Converter !!", bg="lavender", width=40, height=2, bd=2, relief="ridge", font = ("Lucida Console", 10, "italic"))
49+
greeting.grid(row = 0, column = 1, pady=20, padx=12)
50+
51+
#---input row---
52+
fromlabel = Label(root, text="From", width=10, height = 2)
53+
fromlabel.grid(row = 2, column = 0, padx = 20, pady=20)
54+
55+
input = Entry(root, justify="center")
56+
input.grid(row =2, column = 1, ipady=8)
57+
input.config(font = 8)
58+
59+
ipmenu = OptionMenu(root, ip_opt, "cm", "m", "km", "feet", "miles", "inches", "yards")
60+
ipmenu.grid(row = 2, column = 2)
61+
62+
#---output row---
63+
tolabel = Label(root, text="To", width=10, height = 2)
64+
tolabel.grid(row = 4, column = 0, padx = 20, pady=20)
65+
66+
output = Entry(root, justify="center")
67+
output.grid(row =4, column = 1, ipady=8)
68+
output.config(font = 8)
69+
70+
opmenu = OptionMenu(root, op_opt, "cm", "m", "km", "feet", "miles", "inches", "yards")
71+
opmenu.grid(row = 4, column = 2)
72+
73+
#---convert Button---
74+
convertbtn = Button(root, text="Convert", justify="center", width=10, height = 2, command = convert, bg="lavender", font=("bold", 12))
75+
convertbtn.grid(row = 6, column = 1)
76+
77+
root.mainloop()

0 commit comments

Comments
 (0)