|
| 1 | +__author__ = 'Avinash' |
| 2 | + |
| 3 | +import tkinter as tk |
| 4 | +from functools import partial |
| 5 | + |
| 6 | +#global variable |
| 7 | +tempVal = "Celsius" |
| 8 | + |
| 9 | + |
| 10 | +#getting drop down value |
| 11 | +def store_temp(sel_temp): |
| 12 | + global tempVal |
| 13 | + tempVal = sel_temp |
| 14 | + |
| 15 | + |
| 16 | +#the main conversion |
| 17 | +def call_convert(rlabel1, rlabe12, inputn): |
| 18 | + tem = inputn.get() |
| 19 | + if tempVal == 'Celsius': |
| 20 | + f = float((float(tem) * 9/5) + 32) |
| 21 | + k = float((float(tem) + 273.15)) |
| 22 | + rlabel1.config(text="%f Fahrenheit" % f) |
| 23 | + rlabe12.config(text="%f Kelvin" % k) |
| 24 | + if tempVal == 'Fahrenheit': |
| 25 | + c = float((float(tem) - 32) * 5/9) |
| 26 | + k = c + 273 |
| 27 | + rlabel1.config(text="%f Celsius" % c) |
| 28 | + rlabe12.config(text="%f Kelvin" % k) |
| 29 | + if tempVal == 'Kelvin': |
| 30 | + c = float((float(tem) - 273.15)) |
| 31 | + f = float((float(tem) - 273.15) * 1.8000 + 32.00) |
| 32 | + rlabel1.config(text="%f Celsius" % c) |
| 33 | + rlabe12.config(text="%f Fahrenheit" % f) |
| 34 | + return |
| 35 | + |
| 36 | +#app window configuration and UI |
| 37 | +root = tk.Tk() |
| 38 | +root.geometry('400x150+100+200') |
| 39 | +root.title('Temperature Converter') |
| 40 | +root.configure(background='#09A3BA') |
| 41 | +root.resizable(width=False, height=False) |
| 42 | +root.grid_columnconfigure(1, weight=1) |
| 43 | +root.grid_rowconfigure(0, weight=1) |
| 44 | + |
| 45 | +numberInput = tk.StringVar() |
| 46 | +var = tk.StringVar() |
| 47 | + |
| 48 | +#label and entry field |
| 49 | +input_label = tk.Label(root, text="Enter temperature", background='#09A3BA', foreground="#FFFFFF") |
| 50 | +input_entry = tk.Entry(root, textvariable=numberInput) |
| 51 | +input_label.grid(row=1) |
| 52 | +input_entry.grid(row=1, column=1) |
| 53 | + |
| 54 | +#result label's for showing the other two temperatures |
| 55 | +result_label1 = tk.Label(root, background='#09A3BA', foreground="#FFFFFF") |
| 56 | +result_label1.grid(row=3, columnspan=4) |
| 57 | +result_label2 = tk.Label(root, background='#09A3BA', foreground="#FFFFFF") |
| 58 | +result_label2.grid(row=4, columnspan=4) |
| 59 | + |
| 60 | +#drop down initalization and setup |
| 61 | +dropDownList = ["Celsius", "Fahrenheit", "Kelvin"] |
| 62 | +dropdown = tk.OptionMenu(root, var, *dropDownList, command=store_temp) |
| 63 | +var.set(dropDownList[0]) |
| 64 | +dropdown.grid(row=1, column=3) |
| 65 | +dropdown.config(background='#09A3BA', foreground="#FFFFFF") |
| 66 | +dropdown["menu"].config(background='#09A3BA', foreground="#FFFFFF") |
| 67 | + |
| 68 | +#button click |
| 69 | +call_convert = partial(call_convert, result_label1, result_label2, numberInput) |
| 70 | +result_button = tk.Button(root, text="Convert", command=call_convert, background='#09A3BA', foreground="#FFFFFF") |
| 71 | +result_button.grid(row=2, columnspan=4) |
| 72 | + |
| 73 | +root.mainloop() |
0 commit comments