Learn to Code via Tutorials on Repl.it!

← Back to all posts
How make a GUI Celsius to Fahrenheit Converter in Tkinter for absolute beginners!
LegendaryWolf (698)

In this tutorial, I will teach how to make an app that converts Celsius into Fahrenheit, so let's get started! I wrote it up in a few minutes so there might be some typos and errors

Importing

from tkinter * # Imports everything from tkinter library

Create a window

root = Tk() #let's name the window as root(Tk means window)
root.title("The title for your app") #Sets the title for your application

Let's create a label(header)

lbl = Label(root, text="Celsius to Fahrenheit converter", font=("Arial", 40, "bold"), bg="yellow")# It creates a label. increase the fonsize and change the background color to make it look as a label
lbl.pack() # pack just shows the label, we can also display it as a grid by lbl.grid(), but we don't do it here

Blank space

blank = Label(root)# That's it lol, the root thing in all the elements in the code is to declare that the element is of he window. coz we named the window as root!
blank.pack()

Another Label!

lbl1 = Label(root, text="Enter the Celsius in numbers only to convert",  font=("Arial", 20, "bold"))
lbl1.pack()

Another blank!

blank = Label(root)
blank.pack()

Note: Im doing this in a normal python repl, not a Tkinter repl

There is a type of element named 'Entry'. It's used for input.

Input box (aka Entry in Tkinter)

c = Entry(root, font=('Arial', 20,'bold'))#Font changes the text we typed as the code said
c.pack()
blank =Label(root)
blank.pack()

Button!

btn = Button(text="Convert", font=('Arial', 15, 'italic'), command=convert)#When we click the button, a function gets runned. that's that the command thing does
btn.pack()

blank = Label(root)
blank.pack()

## **The label where the image gets displayed!**
```python
r = Label(text="", font=("Arial", 20, "bold"))
r.pack()

The function!

FROM HERE YOU SHOULD -

The formula -

Celsius / 5 * 8 + 32

Let's take the input ffrom the Entry as an interger and do what is said in a variable!!!

result =   int(c.get())  / 5 * 9 + 32

Now, the result label shud display the result!

r['text'] = result #r is the name of the label for displaying the result

Cool! you did it! if you have any bugs in your code, this is the full code which works -

from tkinter import *
root = Tk()
root.title("Celsius to Fahrenheit")

#Functions
def convert():
  try:
    result =   int(c.get())  / 5 * 9 + 32 
    r['text'] = int(result)
    c.delete(0, END)
  except ValueError:
    r['text'] = "Only enter Numbers"



lbl = Label(root, text="Celsius to Fahrenheit converter", font=("Arial", 40, "bold"), bg="yellow")
lbl.pack()

blank =Label(root)
blank.pack()

lbl1 = Label(root, text="Enter the Celsius in numbers only to convert",  font=("Arial", 20, "bold"), justify = LEFT)
lbl1.pack()

c = Entry(root, font=('Arial', 20,'bold'))
c.pack()

blank =Label(root)
blank.pack()


btn = Button(text="Convert", font=('Arial', 15, 'italic'), command=convert)
btn.pack()

blank = Label(root)
blank.pack()

r = Label(text="", font=("Arial", 20, "bold"))
r.pack()

root.mainloop()

Hope you have a good day :)