-
Notifications
You must be signed in to change notification settings - Fork 0
/
Temp Converter.py
78 lines (61 loc) · 2.19 KB
/
Temp Converter.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import tkinter as tk
from tkinter import messagebox
from functools import partial
# Declaration of global variable
temp_Val = "Celsius"
# getting drop down value
def store_temp(set_temp):
global temp_Val
temp_Val = set_temp
# Conversion of temperature
def call_convert(rlabel1, inputn):
temp = inputn.get()
if temp_Val == 'Celsius':
# Conversion of celsius temperature to fahrenheit
f = float((float(temp) * 9 / 5) + 32)
rlabel1.config(text="%.1f Fahrenheit" % f)
messagebox.showinfo("Temperature Converter",
"Successfully converted to Fahrenheit ", )
if temp_Val == 'Fahrenheit':
# Conversion of fahrenheit temperature
# to celsius
c = float((float(temp) - 32) * 5 / 9)
rlabel1.config(text="%.1f Celsius" % c)
messagebox.showinfo("Temperature Converter",
"Successfully converted to Celsius ")
return
# creating Tk window
root = tk.Tk()
# setting geometry of tk window
root.geometry('400x200')
# Using title() to display a message in the
# dialogue box of the message in the title bar
root.title('Temperature Converter')
# Lay out widgets
root.grid_columnconfigure(1, weight=1)
root.grid_rowconfigure(1, weight=1)
inputNumber = tk.StringVar()
var = tk.StringVar()
# label and entry field
input_label = tk.Label(root, text="Enter temperature")
input_entry = tk.Entry(root, textvariable=inputNumber)
input_label.grid(row=1)
input_entry.grid(row=1, column=1)
result_label = tk.Label(root)
result_label.grid(row=3, columnspan=4)
# drop down setup
dropDownList = ["Celsius", "Fahrenheit"]
drop_down = tk.OptionMenu(root, var, *dropDownList,
command=store_temp)
var.set(dropDownList[0])
drop_down.grid(row=1, column=2)
# button widget
call_convert = partial(call_convert, result_label,
inputNumber)
result_button = tk.Button(root, text="Convert",
command=call_convert)
result_button.grid(row=2, columnspan=2)
# infinite loop which is required to
# run tkinter program infinitely
# until an interrupt occurs
root.mainloop()