Tkinter Example

Created with Sketch.

Tkinter Example

Summary: in this tutorial, you’ll learn how to build a Tkinter temperature converter application.

Introduction to the Temperature Converter application

The following shows the Temperature Converter application that you’re going to build. The application converts a temperature from Fahrenheit to Celsius:

Basically, the application has a label, an entry, and a button. When you enter a temperature in Fahrenheit and click the Convert button, it’ll convert the value in the textbox from Fahrenheit to Celsius.

If you enter a value that cannot be converted to a number, the program will show an error.

To build this application, you follow these steps.

First, import the tkinter module, ttk submodule, and the showerror function from tkinter.messagebox:

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showerror

Code language: Python (python)

Second, create the root window and set its configurations:

# root window
root = tk.Tk()
root.title('Temperature Converter')
root.geometry('300x70')
root.resizable(False, False)

Code language: Python (python)

Third, define a function that converts a temperature from Fahrenheit to Celsius:

def fahrenheit_to_celsius(f):
""" Convert fahrenheit to celsius
"""

return (f - 32) * 5/9

Code language: Python (python)

Fourth, create a frame that holds form fields:

frame = ttk.Frame(root)

Code language: Python (python)

Fifth, define an option that will be used by all the form fields:

options = {'padx': 5, 'pady': 5}

Code language: Python (python)

Sixth, define the label, entry, and button. The label will show the result once you click the Convert button:

# temperature label
temperature_label = ttk.Label(frame, text='Fahrenheit')
temperature_label.grid(column=0, row=0, sticky='W', **options)

# temperature entry
temperature_entry = ttk.Entry(frame, textvariable=temperature)
temperature_entry.grid(column=1, row=0, **options)
temperature_entry.focus()

# convert button
convert_button = ttk.Button(frame, text='Convert')
convert_button.grid(column=2, row=0, sticky='W', **options)
convert_button.configure(command=convert_button_clicked)

# result label
result_label = ttk.Label(frame)
result_label.grid(row=1, columnspan=3, **options)

Code language: Python (python)

Finally, place the frame on the root window and run the mainloop() method:

frame.grid(padx=10, pady=10)
root.mainloop()

Code language: Python (python)

Put it all together.

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showerror

# root window
root = tk.Tk()
root.title('Temperature Converter')
root.geometry('300x70')
root.resizable(False, False)

def fahrenheit_to_celsius(f):
""" Convert fahrenheit to celsius
"""

return (f - 32) * 5/9

# frame
frame = ttk.Frame(root)

# field options
options = {'padx': 5, 'pady': 5}

# temperature label
temperature_label = ttk.Label(frame, text='Fahrenheit')
temperature_label.grid(column=0, row=0, sticky='W', **options)

# temperature entry
temperature = tk.StringVar()
temperature_entry = ttk.Entry(frame, textvariable=temperature)
temperature_entry.grid(column=1, row=0, **options)
temperature_entry.focus()

# convert button

def convert_button_clicked():
""" Handle convert button click event
"""

try:
f = float(temperature.get())
c = fahrenheit_to_celsius(f)
result = f'{f} Fahrenheit = {c:.2f} Celsius'
result_label.config(text=result)
except ValueError as error:
showerror(title='Error', message=error)

convert_button = ttk.Button(frame, text='Convert')
convert_button.grid(column=2, row=0, sticky='W', **options)
convert_button.configure(command=convert_button_clicked)

# result label
result_label = ttk.Label(frame)
result_label.grid(row=1, columnspan=3, **options)

# add padding to the frame and show it
frame.grid(padx=10, pady=10)

# start the app
root.mainloop()

Code language: Python (python)

In this tutorial, you’ve learned how to develop a simple Tkinter application that converts a temperature from Fahrenheit to Celsius.

Leave a Reply

Your email address will not be published. Required fields are marked *