Introduction to Tkinter
Introducing GUIs and unit conversion.
We'll cover the following
Unit errors
NASA launched the Mars Climate Orbiter in 1998 to study changes in the Martian atmosphere and climate. In September 1999, NASA lost communication with the orbiter as it was performing an orbital insertion above Mars. It was later discovered that a piece of software provided results in non-SI units when the specifications sheet and software receiving the calculations expected the results in the SI units. The orbiter crashed into Mars due to the unit conversion error, and the experiment was prematurely concluded. That is an expensive unit error.
GUIs in Python
So far, all of our programs have been run within the script editor, and any results have been printed to the console. A Graphical User Interface called a GUI and pronounced as gooey, is the more advanced way to have a user interact with your program and see its results. There are several different libraries for creating a GUI in Python, such as PyQT
, Tkinter
, Wx
, Kivy
, and many others. Tkinter
is a built-in library for Python, so you will use it to create a GUI to convert values in different pressure units. Its documentation [1] and a guide [2] are linked in the Resources section.
Pressure units
Pressure is defined as force per unit area, so the base unit in SI is Newton per meter squared or Pascal. The US unit is pound-force per square inch or psi. You will use Pint
, a Python units library, to assign and convert different units. The documentation for Pint
can be found under footnote [3]. The GitHub repository for Pint
[4] shows all of the default pressure units you will use later.
This program will go through three iterations. The first time, you will see how building a GUI works so that you will hard-code in the conversion from megapascals, MPa, to pounds per square inch, psi. After that, you will learn how to make a dropdown menu to offer the user the option to convert between any base pressure unit. Finally, you will make a free-form text entry; by doing so, you can harness the full functionality of Pint
and let the user convert between any units of the same dimension, like velocity or distance, not just pressure.
Get hands-on with 1400+ tech skills courses.