Welcome folks today in this blog post we will be making a gui temperature converter
desktop app in tkinter using python. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
app.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 |
import tkinter as tk def fahrenheit_to_celsius(): """Convert the value for Fahrenheit to Celsius and insert the result into lbl_result. """ fahrenheit = ent_temperature.get() celsius = (5/9) * (float(fahrenheit) - 32) lbl_result["text"] = f"{round(celsius, 2)} \N{DEGREE CELSIUS}" # Set-up the window window = tk.Tk() window.title("Temperature Converter") window.resizable(width=True, height=True) # Create the Fahrenheit entry frame with an Entry # widget and label in it frm_entry = tk.Frame(master=window) ent_temperature = tk.Entry(master=frm_entry, width=10) lbl_temp = tk.Label(master=frm_entry, text="\N{DEGREE FAHRENHEIT}") # Layout the temperature Entry and Label in frm_entry # using the .grid() geometry manager ent_temperature.grid(row=0, column=0, sticky="e") lbl_temp.grid(row=0, column=1, sticky="w") # Create the conversion Button and result display Label btn_convert = tk.Button( master=window, text="\N{RIGHTWARDS BLACK ARROW}", command=fahrenheit_to_celsius ) lbl_result = tk.Label(master=window, text="\N{DEGREE CELSIUS}") # Set-up the layout using the .grid() geometry manager frm_entry.grid(row=0, column=0, padx=10) btn_convert.grid(row=0, column=1, pady=10) lbl_result.grid(row=0, column=2, padx=10) # Run the application window.mainloop() |
Now if you execute the above script by typing the below command as shown below
python app.py