This document can help you use Python and Tkinter Themed Tk (ttk) widgets to create an English and metric converter that lets you convert either English values to metric, or metric to English. It displays as a standard window on your desktop. You can customize it by adding specific conversions you might need in your work.
Note: I originally created this converter because I wanted something more convenient than the conversion websites. While working on any project that requires conversions to or from the metric system, I simply run my ttk_EngToMetCnvrtr.py program to display its converter window. Of course, I minimize the converter window whenever I don’t need it.
After you install Python 3 and IDLE for Python 3, you can use Tkinter.Ttk widgets to create and/or modify your English and metric converter. For more information, see How to Enter Data Through Python Tkinter.Ttk.
This document explains how to design your converter window, create your converter program, use your converter and customize it. If you wish to add a new conversion, you need two formulas, one to convert English to metric, the other metric to English. Always select both formulas from a reliable source.
Designing Your Converter Window – To design your converter window, do the following:
1 – Design your converter window by creating a planning grid similar to the first image above. You can use either a paper sketch or a word-processor table.
2 – Identify each numeric variable with a letter “v” followed by its row-and-column address. For example, “v41” identifies the numeric variable at row 4, column 1.
3 – Identify each text variable with a letter “t” followed by its row-and-column address. For example, “t115” identifies the text variable at row 11, column 5.
Creating Your Converter Program – To create your converter program, do either of the following:
* – Copy the second, third, and fourth images above, paste them into a word processor, print them and then type the code into your ttk_EngToMetCnvrtr.py program. (To keep the second- through-fourth images compact for this document, I define only two conversions: inches and centimeters in row 4, and Fahrenheit and Celsius degrees in row 5.)
* – Open my Ttk_EngToMetCnvrtr program, copy it and then paste it into your Ttk_EngToMetCnvrtr.py program. For more information, see Running Python Examples. (In my complete example, I define additional conversions in rows 6 through 14.)
Using Your Converter Program – To use your converter program, do the following:
1 – Start Python 3 to display its Python Shell.
2 – Click “File,” click “Open,” navigate to and select your converter program (such as ttk_EngToMetCnvrtr.py), click “Open,” click “Run” and then click “Run Module” to display a window similar to the fifth image above.
3 – Minimize both Python windows and then reposition the converter window if necessary.
4 – Click either English to Metric or Metric to English to select a direction of conversion.
5 – Type a value into any input field, such as centimeters or grams. You can type values into multiple fields.
6 – Click “Convert” to display the converted value(s).
7 – After you have finished using your converter, click “Exit Program” and then close the Python windows you had minimized in step three.
Customizing Your Converter Program – You can customize your converter program by editing existing rows and/or adding rows. For example, if you need only three conversions, and you never need to convert Fahrenheit and Celsius, you can do the following:
1 – Create your converter program by typing its code from the second through fourth images above.
2 – Edit row 5. In the second image above, for both directions (English to metric and metric to English), edit the text in both t52 text variables and both t55 text variables, and the formulas in both v54 numeric variables.
2 – Create row 6 as follows:
a – In the second image above, for both directions, copy the lines from row 5 and then paste as row-6 lines. In the “def selectEM(args):” section, copy the t52 and t55 lines from row 5, paste them as t62 and t65 lines into row 6, and then edit the t62 and t65 text variables. Repeat in the “def selectME(args):” section. In the “def convert(args):” section, in both directions, copy the v51 and v54 lines from each row 5, paste them as v61 and v64 lines into each row 6, and then edit the formulas in both v64 numeric variables.
b – In the third image above, copy the row-5 variable type definitions v51, t52, v54, and t55, and paste them as row-6 variable type definitions v61, t62, v64, and t65.
c – In the fourth image above, copy “# row 5” and its six lines, paste it as “# row 6” and its six lines, and then edit the row-6 lines. Replace all row-5 definitions with those for row 6. For example, replace each v51 with v61, and each row=5 with row=6.
3 – Test your customized converter by running it in both directions: English to metric and metric to English.
* – Python v3.1.3 Documentation, “Tkinter.Ttk – Tk Themed Widgets”, Python Software Foundation
* – Penn State, “English/Metric Conversion”, College of Agricultural Sciences