Temperature Converter using Tkinter

20 Jan, 2022
Prerequisites: Python GUI – tkinterPython Tkinter is a GUI programming package or built-in library. Tkinter provides the Tk GUI toolkit with a potent object-oriented interface. Python with Tkinter is the fastest and easiest way to create GUI applications. Creating a GUI using Tkinter is an easy task.Approach:  
Importing the module – tkinter, functools from partial
Create the main window
Add number of widgets to the main window:Button, Entry, Label
Displaying message
Apply the event trigger on the widgets.
Below is the implementation.  
python3
import tkinter as tkfrom tkinter import messageboxfrom functools import partial  # Declaration of global variabletemp_Val = "Celsius" # getting drop down valuedef store_temp(set_temp):    global temp_Val    temp_Val = set_temp # Conversion of  temperaturedef 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 windowroot = tk.Tk() # setting geometry of tk windowroot.geometry('300x150 + 600 + 200') # Using title() to display a message in the# dialogue box of the message in the title barroot.title('Temperature Converter') # Lay out widgetsroot.grid_columnconfigure(1, weight = 1)root.grid_rowconfigure(1, weight = 1) inputNumber = tk.StringVar()var = tk.StringVar() # label and entry fieldinput_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 setupdropDownList = ["Celsius", "Fahrenheit"]drop_down = tk.OptionMenu(root, var, *dropDownList,                          command = store_temp)var.set(dropDownList[0])drop_down.grid(row = 1, column = 2) # button widgetcall_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 occursroot.mainloop()
Output: 
 
sumitgumber28
Python Tkinter-exercises
Python-tkinter
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to iterate through Excel rows in Python?
Rotate axis tick labels in Seaborn and Matplotlib
Deque in Python
Queue in Python
Defaultdict in Python
Check if element exists in list in Python
Python Classes and Objects
Bar Plot in Matplotlib
reduce() in Python
Python | Get unique values from a list