Python Temperature Converter: (°C) to (°F) and Vice Versa

It's a user-friendly Python Tkinter temperature converter.

Introduction

Temperature conversion is a common task in various fields, including science, engineering, and everyday life. Python, being a versatile and popular programming language, provides a range of tools and libraries to simplify such conversions. In this article, we will explore how to build a temperature converter using Python’s Tkinter library, which allows us to create graphical user interfaces (GUIs) effortlessly.

Prerequisites

To follow along with this tutorial, you should have a basic understanding of Python programming and the Tkinter library. If you’re new to Tkinter, don’t worry! We will provide step-by-step instructions to help you grasp the concepts and build your temperature converter.

Getting Started

Before we dive into the code, make sure you have Python installed on your system. Tkinter is a standard library that comes bundled with Python, so no additional installation is required. Open your preferred integrated development environment (IDE) or text editor, and let’s begin!

Creating the GUI

To start, we need to import the necessary modules and create a Tkinter window.


import tkinter as tk

window = tk.Tk()
window.title("Temperature Converter")
window.geometry("420x180")

Implementing Conversion Logic

Now, let’s write a function that performs the temperature conversion. We will use the Celsius to Fahrenheit conversion formula: Fahrenheit = Celsius * 9/5 + 32, and Fahrenheit to Celsius conversion formula: Celsius = (Fahrenheit – 32) * 5 / 9.


# Celsius to Fahrenheit conversion function
def celsius_to_fahrenheit():
    celsius = float(celsius_entry.get())
    fahrenheit = (celsius * 9 / 5) + 32
    result_label.config(text=f"{celsius}°C = {fahrenheit}°F")

# Fahrenheit to Celsius conversion function
def fahrenheit_to_celsius():
    fahrenheit = float(fahrenheit_entry.get())
    celsius = (fahrenheit - 32) * 5 / 9
    result_label.config(text=f"{fahrenheit}°F = {celsius}°C")

In the above code, we define two conversion functions, `celsius_to_fahrenheit()` and `fahrenheit_to_celsius()`, which perform the actual temperature conversions based on the input values.

Designing the User Interface

Now, we need to design the user interface by adding labels, entry fields, and buttons. Modify your code as follows:


# Celsius to Fahrenheit conversion section
celsius_label = tk.Label(window, text="Celsius")
celsius_label.pack()

celsius_entry = tk.Entry(window)
celsius_entry.pack()

celsius_to_fahrenheit_button = tk.Button(window, 
text="Convert to Fahrenheit", command=celsius_to_fahrenheit)

celsius_to_fahrenheit_button.pack()

# Fahrenheit to Celsius conversion section
fahrenheit_label = tk.Label(window, text="Fahrenheit")
fahrenheit_label.pack()

fahrenheit_entry = tk.Entry(window)
fahrenheit_entry.pack()

fahrenheit_to_celsius_button = tk.Button(window, 
text="Convert to Celsius", command=fahrenheit_to_celsius)

fahrenheit_to_celsius_button.pack()

# Result section
result_label = tk.Label(window, text="")
result_label.pack()

Here, we create labels and entry fields for Celsius and Fahrenheit values. Two buttons, “Convert to Fahrenheit” and “Convert to Celsius”, are added to trigger the conversion functions. Finally, we create a label to display the result.

Finalizing the GUI

To complete our temperature converter, we’ll add a main loop that keeps the window open until the user closes it.


window.mainloop()

This line of code starts the Tkinter event loop, which listens for user input and responds accordingly. Save the file and execute it using a Python interpreter.

Output

The program outputs like the following.

this is a python-based temperature converter application, built using tkinter library, offers celsius to fahrenheit conversion and vice versa.

Conclusion

Congratulations! You have successfully built a temperature converter using Python and Tkinter. By combining the power of Python with the simplicity of Tkinter, you have created a user-friendly tool for converting temperatures between Celsius and Fahrenheit.

Remember, Python and Tkinter offer endless possibilities for creating versatile graphical applications. With a little creativity and exploration, you can build a wide range of GUI-based tools to simplify various tasks. Happy coding!

Share your love
Subhankar Rakshit
Subhankar Rakshit

Hey there! I’m Subhankar Rakshit, the brains behind PySeek. I’m a Post Graduate in Computer Science. PySeek is where I channel my love for Python programming and share it with the world through engaging and informative blogs.

Articles: 147