Skip to content

Commit 7afbd0d

Browse files
author
Serena
committed
added temperature and distance converter to conversion scripts
1 parent acd82b7 commit 7afbd0d

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Temperature and Distance Converter
2+
3+
Python Tkinter program that allows a variety of distance and temperature programsconversions.
4+
5+
## Modules
6+
7+
Requires Tkinter. Use the following command if not previously installed:
8+
9+
```
10+
pip install -r requirements.txt
11+
```
12+
13+
## Use
14+
15+
Run the script in an environment that supports tkinter GUI.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from fractions import Fraction
2+
import tkinter as tk
3+
4+
convert = {"Distance": {"Inches to Centimeters": lambda x: x * 2.54,
5+
"Centimeters to Inches": lambda x: x / 2.54,
6+
"Feet to Inches": lambda x: x * 12,
7+
"Inches to Feet": lambda x: x / 12,
8+
"Meters to Feet": lambda x: ((100 / 2.54) / 12) * x,
9+
"Feet to Meters": lambda x: x / ((100 / 2.54) / 12),
10+
"Inches to Meters": lambda x: (x * 2.54) / 100,
11+
"Meters to Inches": lambda x: (100 / 2.54) * x,
12+
"Miles to Feet": lambda x: x * 5280,
13+
"Feet to Miles": lambda x: x / 5280,
14+
"Miles to Yards": lambda x: x * 1760,
15+
"Yards to Miles": lambda x: x / 1760,
16+
"Miles to Kilometers": lambda x: x * 1.609,
17+
"Kilometers to Miles": lambda x: x / 1.609},
18+
"Temperature": {"Fahrenheit to Celsius": lambda x: (x - 32) * (5/9),
19+
"Celsius to Fahrenheit": lambda x: x * (9/5) + 32}}
20+
21+
window = tk.Tk()
22+
window.title("Converter")
23+
window.geometry("400x400")
24+
bg_color, fg_color = "white", "black"
25+
window.configure(bg = bg_color)
26+
27+
entry_frame = tk.Frame(window, bg = bg_color)
28+
lbl = tk.Label(window, bg = bg_color, fg = fg_color)
29+
lbl.pack()
30+
for i in 'entry_lbl1', 'entry_lbl2', 'error_lbl', 'entry2':
31+
globals()[i] = tk.Label(entry_frame, bg = bg_color, fg = fg_color)
32+
entry1 = tk.Entry(entry_frame)
33+
def conversion(event):
34+
try:
35+
entry2['text'] = convert[category][choice](float(entry1.get()))
36+
error_lbl['text'] = ''
37+
except:
38+
error_lbl['text'] = "Sorry, please input a number."
39+
entry1.delete(0, tk.END); entry2['text'] = ''
40+
41+
entry1.bind("<Return>", conversion)
42+
43+
def go_back():
44+
for i in window.children:
45+
if '!radiobutton' in i:
46+
window.children[i].pack_forget()
47+
entry_frame.pack_forget(); back_btn.pack_forget()
48+
start()
49+
back_btn = tk.Button(window, text = 'BACK', command = go_back)
50+
51+
def make_rbtns(List):
52+
rbtns = {}
53+
w = max([len(i) for i in List]) + 2
54+
for i in List:
55+
rbtns[i] = {}
56+
rbtns[i]['var'] = tk.StringVar()
57+
rbtns[i]['button'] = tk.Radiobutton(window, text = i, value = i, var = rbtns[i]['var'], fg = fg_color, bg = bg_color, width = w, anchor = 'w')
58+
rbtns[i]['button'].pack()
59+
if step != 1:
60+
back_btn.pack()
61+
return rbtns
62+
63+
def chosen(rbtns):
64+
for i in rbtns:
65+
rbtns[i]['button'].pack_forget()
66+
if rbtns[i]['var'].get() != '':
67+
choice = i
68+
return choice
69+
70+
def entered():
71+
global choice
72+
choice = chosen(choices)
73+
lbl['text'] = "Enter your conversions below:"
74+
entry_lbl1['text'] = choice.split("to")[0]; entry_lbl2['text'] = choice.split('to')[1]
75+
entry_lbl1.grid(row = 1, column = 0); entry_lbl2.grid(row = 2, column = 0)
76+
entry1.grid(row = 1, column = 1); entry2.grid(row = 2, column = 1)
77+
entry1.focus()
78+
error_lbl.grid(row = 3, column = 1)
79+
entry_frame.pack()
80+
back_btn.pack_forget(); back_btn.pack()
81+
82+
def init():
83+
global choices, category, step
84+
step = 2
85+
category = chosen(categories)
86+
choices = make_rbtns(convert[category])
87+
for i in choices:
88+
choices[i]['button']['command'] = entered
89+
90+
def start():
91+
global categories, step
92+
step = 1
93+
categories = make_rbtns(["Distance", "Temperature"])
94+
for i in categories:
95+
categories[i]['button']['command'] = init
96+
lbl['text'] = "What would you like to convert?"
97+
98+
start()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tkinter

0 commit comments

Comments
 (0)