{"id":1499,"date":"2020-12-07T07:39:26","date_gmt":"2020-12-07T07:39:26","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1499"},"modified":"2020-12-11T01:13:56","modified_gmt":"2020-12-11T01:13:56","slug":"tkinter-example","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-example\/","title":{"rendered":"Tkinter Example"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to build a Tkinter temperature converter application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-temperature-converter-application'>Introduction to the Temperature Converter application <a href=\"#introduction-to-the-temperature-converter-application\" class=\"anchor\" id=\"introduction-to-the-temperature-converter-application\" title=\"Anchor for Introduction to the Temperature Converter application\">#<\/a><\/h2>\n\n\n\n<p>The following shows the Temperature Converter application that you&#8217;re going to build. The application converts a temperature from Fahrenheit to Celsius:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"102\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Example.png\" alt=\"\" class=\"wp-image-1500\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Example.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-Example-300x101.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure><\/div>\n\n\n\n<p>Basically, the application has a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-label\/\">label<\/a>, an <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-entry\/\">entry<\/a>, and a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-button\/\">button<\/a>. When you enter a temperature in Fahrenheit and click the <code>Convert<\/code> button, it&#8217;ll convert the value in the textbox from Fahrenheit to Celsius.<\/p>\n\n\n\n<p>If you enter a value that cannot be converted to a number, the program will show an error.<\/p>\n\n\n\n<p>To build this application, you follow these steps.<\/p>\n\n\n\n<p>First, import the <code>tkinter<\/code> module, <code>ttk<\/code> submodule, and the <code>showerror<\/code> function from <code>tkinter.messagebox<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showerror<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create the root window and set its configurations:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Temperature Converter'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x70'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, define a function that converts a temperature from Fahrenheit to Celsius:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fahrenheit_to_celsius<\/span><span class=\"hljs-params\">(f)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" Convert fahrenheit to celsius\n    \"\"\"<\/span>\n    <span class=\"hljs-keyword\">return<\/span> (f - <span class=\"hljs-number\">32<\/span>) * <span class=\"hljs-number\">5<\/span>\/<span class=\"hljs-number\">9<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, create a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-frame\/\">frame<\/a> that holds form fields:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">frame = ttk.Frame(root)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fifth, define an option that will be used by all the form fields:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">options = {<span class=\"hljs-string\">'padx'<\/span>: <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">'pady'<\/span>: <span class=\"hljs-number\">5<\/span>}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Sixth, define the label, entry, and button. The label will show the result once you click the <code>Convert<\/code> button:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># temperature label<\/span>\ntemperature_label = ttk.Label(frame, text=<span class=\"hljs-string\">'Fahrenheit'<\/span>)\ntemperature_label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'W'<\/span>, **options)\n\n<span class=\"hljs-comment\"># temperature entry<\/span>\ntemperature_entry = ttk.Entry(frame, textvariable=temperature)\ntemperature_entry.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, **options)\ntemperature_entry.focus()\n\n<span class=\"hljs-comment\"># convert button<\/span>\nconvert_button = ttk.Button(frame, text=<span class=\"hljs-string\">'Convert'<\/span>)\nconvert_button.grid(column=<span class=\"hljs-number\">2<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'W'<\/span>, **options)\nconvert_button.configure(command=convert_button_clicked)\n\n<span class=\"hljs-comment\"># result label<\/span>\nresult_label = ttk.Label(frame)\nresult_label.grid(row=<span class=\"hljs-number\">1<\/span>, columnspan=<span class=\"hljs-number\">3<\/span>, **options)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, place the frame on the root window and run the <code>mainloop()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">frame.grid(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Put it all together.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n<span class=\"hljs-keyword\">from<\/span> tkinter <span class=\"hljs-keyword\">import<\/span> ttk\n<span class=\"hljs-keyword\">from<\/span> tkinter.messagebox <span class=\"hljs-keyword\">import<\/span> showerror\n\n<span class=\"hljs-comment\"># root window<\/span>\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Temperature Converter'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x70'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">fahrenheit_to_celsius<\/span><span class=\"hljs-params\">(f)<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\" Convert fahrenheit to celsius\n    \"\"\"<\/span>\n    <span class=\"hljs-keyword\">return<\/span> (f - <span class=\"hljs-number\">32<\/span>) * <span class=\"hljs-number\">5<\/span>\/<span class=\"hljs-number\">9<\/span>\n\n\n<span class=\"hljs-comment\"># frame<\/span>\nframe = ttk.Frame(root)\n\n\n<span class=\"hljs-comment\"># field options<\/span>\noptions = {<span class=\"hljs-string\">'padx'<\/span>: <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">'pady'<\/span>: <span class=\"hljs-number\">5<\/span>}\n\n<span class=\"hljs-comment\"># temperature label<\/span>\ntemperature_label = ttk.Label(frame, text=<span class=\"hljs-string\">'Fahrenheit'<\/span>)\ntemperature_label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'W'<\/span>, **options)\n\n<span class=\"hljs-comment\"># temperature entry<\/span>\ntemperature = tk.StringVar()\ntemperature_entry = ttk.Entry(frame, textvariable=temperature)\ntemperature_entry.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, **options)\ntemperature_entry.focus()\n\n<span class=\"hljs-comment\"># convert button<\/span>\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">convert_button_clicked<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-string\">\"\"\"  Handle convert button click event \n    \"\"\"<\/span>\n    <span class=\"hljs-keyword\">try<\/span>:\n        f = float(temperature.get())\n        c = fahrenheit_to_celsius(f)\n        result = <span class=\"hljs-string\">f'<span class=\"hljs-subst\">{f}<\/span> Fahrenheit = <span class=\"hljs-subst\">{c:<span class=\"hljs-number\">.2<\/span>f}<\/span> Celsius'<\/span>\n        result_label.config(text=result)\n    <span class=\"hljs-keyword\">except<\/span> ValueError <span class=\"hljs-keyword\">as<\/span> error:\n        showerror(title=<span class=\"hljs-string\">'Error'<\/span>, message=error)\n\n\nconvert_button = ttk.Button(frame, text=<span class=\"hljs-string\">'Convert'<\/span>)\nconvert_button.grid(column=<span class=\"hljs-number\">2<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=<span class=\"hljs-string\">'W'<\/span>, **options)\nconvert_button.configure(command=convert_button_clicked)\n\n<span class=\"hljs-comment\"># result label<\/span>\nresult_label = ttk.Label(frame)\nresult_label.grid(row=<span class=\"hljs-number\">1<\/span>, columnspan=<span class=\"hljs-number\">3<\/span>, **options)\n\n<span class=\"hljs-comment\"># add padding to the frame and show it<\/span>\nframe.grid(padx=<span class=\"hljs-number\">10<\/span>, pady=<span class=\"hljs-number\">10<\/span>)\n\n\n<span class=\"hljs-comment\"># start the app<\/span>\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this tutorial, you&#8217;ve learned how to develop a simple Tkinter application that converts a temperature from Fahrenheit to Celsius.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1499\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-example\/\"\n\t\t\t\tdata-post-title=\"Tkinter Example\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1499\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-example\/\"\n\t\t\t\tdata-post-title=\"Tkinter Example\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use the Tkinter to build a simple temperature converter application.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":40,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1499","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1499","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=1499"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1499\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=1499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}