{"id":1237,"date":"2020-11-25T01:37:43","date_gmt":"2020-11-25T01:37:43","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1237"},"modified":"2025-04-03T08:31:49","modified_gmt":"2025-04-03T08:31:49","slug":"tkinter-hello-world","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-hello-world\/","title":{"rendered":"Tkinter Hello, World!"},"content":{"rendered":"\r\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn step-by-step how to develop the <code>Tkinter<\/code> &#8220;Hello, World!&#8221; program.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='creating-a-window'>Creating a window <a href=\"#creating-a-window\" class=\"anchor\" id=\"creating-a-window\" title=\"Anchor for Creating a window\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>The following program shows how to display a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/\">window<\/a> on the screen:<\/p>\r\n\r\n\r\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\r\n\r\nroot = tk.Tk()\r\nroot.mainloop()<\/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>\r\n\r\n\r\n<p>If you execute the program, you&#8217;ll see the following window:<\/p>\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"200\" height=\"230\" class=\"wp-image-1238\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/tkinter-hello-world-root-window.png\" alt=\"\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n<p>How it works.<\/p>\r\n\r\n\r\n\r\n<p>First, import the <code>tkinter<\/code> module as <code>tk<\/code> to the program:<\/p>\r\n\r\n\r\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-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk<\/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>\r\n\r\n\r\n<p>Second, create an instance of the <code>tk.Tk<\/code> class that will create the application window:<\/p>\r\n\r\n\r\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\">root = tk.Tk()<\/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>\r\n\r\n\r\n<p>By convention, the main window in Tkinter is called <code>root<\/code>. But you can use any other name like <code>window<\/code>.<\/p>\r\n\r\n\r\n\r\n<p>Third, call the <code>mainloop()<\/code> method of the main window object:<\/p>\r\n\r\n\r\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\">root.mainloop()<\/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>\r\n\r\n\r\n<p>The <code>mainloop()<\/code> method ensures the main window remains visible on the screen.<\/p>\r\n\r\n\r\n\r\n<p>If you don&#8217;t call the <code>mainloop()<\/code> method, the main window will display and disappear almost instantly &#8211; too quickly to perceive its appearance.<\/p>\r\n\r\n\r\n\r\n<p>Additionally, the <code>mainloop()<\/code> method ensures the main window continues to display and run until you close it.<\/p>\r\n\r\n\r\n\r\n<p>Typically, in a Tkinter program, you place the call to the <code>mainloop()<\/code> method as the last statement after creating the widgets.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='troubleshooting'>Troubleshooting <a href=\"#troubleshooting\" class=\"anchor\" id=\"troubleshooting\" title=\"Anchor for Troubleshooting\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>The <code>tkinter<\/code> module is a built-in Python module. But sometimes, it is not the case. For example, on Ubuntu, you may encounter the following error:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">ImportError: No <span class=\"hljs-built_in\">module<\/span> named Tkinter<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<p>In this case, you need to install <code>tkinter<\/code> module using the following command line:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">sudo apt-<span class=\"hljs-keyword\">get<\/span> install python3-tk<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='displaying-a-label'>Displaying a label <a href=\"#displaying-a-label\" class=\"anchor\" id=\"displaying-a-label\" title=\"Anchor for Displaying a label\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>Now, let&#8217;s place a component on the window. In Tkinter, these components are referred to as <strong>widgets<\/strong>.<\/p>\r\n\r\n\r\n\r\n<p>The following code adds a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-label\/\">label <\/a>widget to the main window:<\/p>\r\n\r\n\r\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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\n\r\nroot = tk.Tk()\r\n\r\n<span class=\"hljs-comment\"># place a label on the root window<\/span>\r\nmessage = tk.Label(root, text=<span class=\"hljs-string\">\"Hello, World!\"<\/span>)\r\nmessage.pack()\r\n\r\n<span class=\"hljs-comment\"># keep the window displaying<\/span>\r\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>\r\n\r\n\r\n<p class=\"note\">Keep in mind that you&#8217;ll learn more about the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-label\/\"><code>Label<\/code> widget<\/a> in the upcoming tutorial.<\/p>\r\n\r\n\r\n\r\n<p>If you run the program, you&#8217;ll see the following window:<\/p>\r\n\r\n\r\n<div class=\"wp-block-image\">\r\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"186\" height=\"51\" class=\"wp-image-1239\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/tkinter-hello-world-label.png\" alt=\"Tkinter Hello World\" \/><\/figure>\r\n<\/div>\r\n\r\n\r\n<p>How it works:<\/p>\r\n\r\n\r\n\r\n<p>To create a widget that belongs to a window, you use the following syntax:<\/p>\r\n\r\n\r\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\">widget = WidgetName(master, **kw)<\/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>\r\n\r\n\r\n<p>In this syntax:<\/p>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>The <code>master<\/code> is the parent <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/\">window<\/a> or <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-frame\/\">frame<\/a> where you want to place the widget.<\/li>\r\n\r\n\r\n\r\n<li>The <code>kw<\/code> is one or more <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-keyword-arguments\/\">keyword arguments<\/a> that specify the configurations of the widget.<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<p>In the program, the following creates a <code>Label<\/code> widget placed on the <code>root<\/code> window:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">message = tk.Label(root, text=<span class=\"hljs-string\">\"Hello, World!\"<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><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>\r\n\r\n\r\n<p>The following statement positions the <code>Label<\/code> on the main window:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">message.pack()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><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>\r\n\r\n\r\n<p>Note that you&#8217;ll learn more about the <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-pack\/\">pack()<\/a><\/code> method later. If you don&#8217;t call the <code>pack()<\/code> method, Tkinter still creates the widget. However, the widget will not be visible.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='fixing-the-blur-ui-on-windows'>Fixing the blur UI on Windows <a href=\"#fixing-the-blur-ui-on-windows\" class=\"anchor\" id=\"fixing-the-blur-ui-on-windows\" title=\"Anchor for Fixing the blur UI on Windows\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<p>If you find the text and UI are blurry on Windows, you can use the\u00a0<code>ctypes<\/code>\u00a0Python library to fix it.<\/p>\r\n\r\n\r\n\r\n<p>First, import the <code>ctypes<\/code> module:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> ctypes <span class=\"hljs-keyword\">import<\/span> windll<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><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>\r\n\r\n\r\n<p>Second, call the <code>SetProcessDpiAwareness()<\/code> function:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">windll.shcore.SetProcessDpiAwareness(<span class=\"hljs-number\">1<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><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>\r\n\r\n\r\n<p>If you want the application to run across platforms such as Windows, macOS, and Linux, you can place the above code in a <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-try-except-finally\/\">try...finally<\/a><\/code> block:<\/p>\r\n\r\n\r\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" 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\r\n\r\n\r\nroot = tk.Tk()\r\n\r\n<span class=\"hljs-comment\"># place a label on the root window<\/span>\r\nmessage = tk.Label(root, text=<span class=\"hljs-string\">\"Hello, World!\"<\/span>)\r\nmessage.pack()\r\n\r\n<span class=\"hljs-comment\"># keep the window displaying<\/span>\r\n<span class=\"hljs-keyword\">try<\/span>:\r\n    <span class=\"hljs-keyword\">from<\/span> ctypes <span class=\"hljs-keyword\">import<\/span> windll\r\n    windll.shcore.SetProcessDpiAwareness(<span class=\"hljs-number\">1<\/span>)\r\n<span class=\"hljs-keyword\">finally<\/span>:\r\n    root.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><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>\r\n\r\n\r\n<p>On Windows, the code in the <code>try<\/code> block will work properly. On macOS or Linux, it will fail. However, the code in the <code>finally<\/code> lock will always execute that displays the main window.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Import the <code>tkinter<\/code> module to create a Tkinter desktop application.<\/li>\r\n\r\n\r\n\r\n<li>Use <code>Tk<\/code> class to create the main window and call the <code>mainloop()<\/code> method to main the display of the main window.<\/li>\r\n\r\n\r\n\r\n<li>In Tkinter, components are called widgets.<\/li>\r\n<\/ul>\r\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=\"1237\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-hello-world\/\"\n\t\t\t\tdata-post-title=\"Tkinter Hello, World!\"\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=\"1237\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-hello-world\/\"\n\t\t\t\tdata-post-title=\"Tkinter Hello, World!\"\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 develop the Tkinter &#8220;Hello, World!&#8221; program.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1237","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1237","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=1237"}],"version-history":[{"count":5,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1237\/revisions"}],"predecessor-version":[{"id":7435,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1237\/revisions\/7435"}],"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=1237"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}