Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Building a Temperature Converter

00:00 Building a Temperature Converter In this section of the course, you’ll build a temperature converter application that allows the user to input temperature in degrees Fahrenheit and click a button to convert the temperature to degrees Celsius.

00:14 You’ll walk through the code step by step, and it will also be provided in the course materials. Before you start coding, you’ll design the app. You’ll need three elements: Entry, a widget called ent_temperature for entering the Fahrenheit value. Label, a widget called lbl_result to display the Celsius result, and Button, a widget called btn_convert that reads the value from the Entry widget, converts it from Fahrenheit to Celsius, and then sets the text of the Label widget to the result when clicked.

00:49 You can arrange these in a grid with a single row and one column for each widget. That will get you a minimally working application, but it’s not very user-friendly.

00:58 Everything needs to have labels. You’ll put a label directly in the right of the ent_temperature widget containing the Fahrenheit symbol so that the user knows that the value ent_temperature should be in degrees Fahrenheit.

01:12 To do this, you’ll set the label text as seen on screen, which uses Python’s named Unicode character support to display the Fahrenheit symbol. You can give button_convert some flare by setting its text to the value seen on screen, which displays a black arrow pointing to the right.

01:31 You’ll also make sure that lbl_result has the Celsius symbol following the label text to indicate that the result is in degrees Celsius. So now that you know what widgets you’ll need and what the window’s going to look like, you can start coding it. First, import tkinter and create a new window.

01:55 window.title sets the title of an existing window while window.resizable with both arguments set to false, makes the window have a fixed size.

02:04 When you finally run this application, the window will have the text “Temperature Converter” in the title bar. Next, create a Frame widget

02:17 assign a couple of new widgets to it, an Entry widget called ent_temperature, and a Label widget called lbl_temp.

02:29 The user will enter the Fahrenheit value in ent_temperature and lbl_temp is used to label ent_temperature with the Fahrenheit symbol.

02:38 The frame entry container groups ent_temperature and label_temp together. You’ll want lbl_temp to be placed directly to the right of ent_temperature.

02:48 You can lay them out in frame entry using the .grid() geometry manager with one row and two columns.

02:58 You set the sticky parameter to e for ent_temperature so that it always sticks to the rightmost edge of its grid cell. You also set sticky to w for lbl_temp to keep it stuck to the leftmost edge of its grid cell.

03:12 This ensures that lbl_temp is always located immediately to the right of ent_temperature. Now make the btn_convert and the lbl_result for converting the temperature entered into ent_temperature and displaying the results.

03:39 Like form entry both btn_convert and lbl_result are assigned to window. Together, these three widgets make up the three cells in the main application grid.

03:51 Now use .grid() to lay them out.

04:07 Finally, you’ll need to run the main loop.

04:14 When you run the program, you can see it looks good, but the button doesn’t do anything. So let’s change that. At the top of the script, add a function called fahrenheit_to_celsius().

04:37 This function reads the value from ent_temperature, converts it from Fahrenheit to Celsius, and then displays the result in lbl_result.

04:54 The final step is to go to the definition of btn_convert and set its command parameter to Fahrenheit_to_Celsius(),

05:06 and that’s it. When you run the program, you’ll see that in just 34 lines of code, you’ve created a fully functional temperature converter with a GUI that’s cross-platform.

05:22 That’s pretty impressive but in the next section of the course, you’ll take it to the next level by making a text editor.

Become a Member to join the conversation.