{"id":1321,"date":"2020-11-29T07:34:16","date_gmt":"2020-11-29T07:34:16","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1321"},"modified":"2025-04-03T04:40:14","modified_gmt":"2025-04-03T04:40:14","slug":"tkinter-window","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/","title":{"rendered":"Tkinter Window"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to manipulate various attributes of a Tkinter window.<\/p>\n\n\n\n<p>The following simple program shows the main window:<\/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\nroot = tk.Tk()\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>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"202\" height=\"232\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-window-default.png\" alt=\"\" class=\"wp-image-1323\"\/><\/figure>\n\n\n\n<p>The main window has a title that defaults to <code>tk<\/code>. It also has three system buttons including Minimize, Maximize, and Close.<\/p>\n\n\n\n<p>Let&#8217;s learn how to change the attributes of the main window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='changing-the-window-title'>Changing the window title <a href=\"#changing-the-window-title\" class=\"anchor\" id=\"changing-the-window-title\" title=\"Anchor for Changing the window title\">#<\/a><\/h2>\n\n\n\n<p>To change the window&#8217;s title, you use the <code>title()<\/code> method like this:<\/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\">root.title(new_title)<\/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>For example, the following changes the title of the root window to <code>'Tkinter Window Demo'<\/code>:<\/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-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Window Demo'<\/span>)\n\nroot.mainloop()<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"312\" height=\"231\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-window-title.png\" alt=\"\" class=\"wp-image-1322\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-window-title.png 312w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-window-title-300x222.png 300w\" sizes=\"auto, (max-width: 312px) 100vw, 312px\" \/><\/figure>\n\n\n\n<p>To get the current title of a window, you use the <code>title()<\/code> method with no argument:<\/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\">title = root.title()<\/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<h2 class=\"wp-block-heading\" id='changing-window-size-and-location'>Changing window size and location <a href=\"#changing-window-size-and-location\" class=\"anchor\" id=\"changing-window-size-and-location\" title=\"Anchor for Changing window size and location\">#<\/a><\/h2>\n\n\n\n<p>In Tkinter, the position and size of a window on the screen is determined by its <em>geometry<\/em>. <\/p>\n\n\n\n<p>The following shows the geometry specification:<\/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\">widthxheight\u00b1x\u00b1y<\/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<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"630\" height=\"496\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-Window-Geometry.png\" alt=\"Tkinter Window Geometry\" class=\"wp-image-2206\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-Window-Geometry.png 630w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-Window-Geometry-300x236.png 300w\" sizes=\"auto, (max-width: 630px) 100vw, 630px\" \/><\/figure>\n\n\n\n<p>In this specification:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>width<\/code> represents the window&#8217;s width in pixels.<\/li>\n\n\n\n<li>The <code>height<\/code> represents the window&#8217;s height in pixels.<\/li>\n\n\n\n<li>The <code>x<\/code> parameter denotes the window&#8217;s horizontal position. For example, a value of <code>+50<\/code> signifies the left edge of the window should be positioned 50 pixels from the left edge of the screen. Conversely, a value of <code>-50<\/code> indicates the right edge of the window should be 50 pixels from the right edge of the screen.<\/li>\n\n\n\n<li>The <code>y<\/code> parameter denotes the window&#8217;s vertical position. For example, a value of <code>+50<\/code> signifies the top edge of the window should be positioned 50 pixels below the top of the screen. Conversely, a value of <code>-50<\/code> indicates the bottom edge of the window should be 50 pixels above the bottom of the screen.<\/li>\n<\/ul>\n\n\n\n<p>To change the size and position of a window, you use the <code>geometry()<\/code> method:<\/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\">root.geometry(new_geometry)<\/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>The following example changes the size of the window to <code>600x400<\/code> and the position of the window to 50 pixels from the top and left of the screen:<\/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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n\n\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Window Demo'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'600x400+50+50'<\/span>)\n\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>Sometimes, you may want to center the window on the screen. The following program illustrates how to do it:<\/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\n\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Window - Center'<\/span>)\n\nwindow_width = <span class=\"hljs-number\">300<\/span>\nwindow_height = <span class=\"hljs-number\">200<\/span>\n\n<span class=\"hljs-comment\"># get the screen dimension<\/span>\nscreen_width = root.winfo_screenwidth()\nscreen_height = root.winfo_screenheight()\n\n<span class=\"hljs-comment\"># find the center point<\/span>\ncenter_x = int(screen_width\/<span class=\"hljs-number\">2<\/span> - window_width \/ <span class=\"hljs-number\">2<\/span>)\ncenter_y = int(screen_height\/<span class=\"hljs-number\">2<\/span> - window_height \/ <span class=\"hljs-number\">2<\/span>)\n\n<span class=\"hljs-comment\"># set the position of the window to the center of the screen<\/span>\nroot.geometry(<span class=\"hljs-string\">f'<span class=\"hljs-subst\">{window_width}<\/span>x<span class=\"hljs-subst\">{window_height}<\/span>+<span class=\"hljs-subst\">{center_x}<\/span>+<span class=\"hljs-subst\">{center_y}<\/span>'<\/span>)\n\n\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>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, get the screen width and height using the <code>winfo_screenwidth()<\/code> and <code>winfo_screenheight()<\/code> methods.<\/li>\n\n\n\n<li>Second, calculate the center coordinate based on the screen and window&#8217;s width and height.<\/li>\n\n\n\n<li>Finally, set the geometry for the main window using the <code>geometry()<\/code> method.<\/li>\n<\/ul>\n\n\n\n<p>If you want to get the current geometry of a window, you can use the <code>geometry()<\/code> method without providing any argument:<\/p>\n\n\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\">root.geometry()<\/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>\n\n\n<h2 class=\"wp-block-heading\" id='resizing-behavior'>Resizing behavior <a href=\"#resizing-behavior\" class=\"anchor\" id=\"resizing-behavior\" title=\"Anchor for Resizing behavior\">#<\/a><\/h2>\n\n\n\n<p>By default, you can resize the width and height of a window. To prevent the window from resizing, you can use the <code>resizable()<\/code> method:<\/p>\n\n\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\">root.resizable(width,height)<\/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>\n\n\n<p>The <code>resizable()<\/code> method has two parameters that specify whether the width and height of the window can be resizable. <\/p>\n\n\n\n<p>The following shows how to make the window with a fixed size:<\/p>\n\n\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\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\n\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Window Demo'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'600x400+50+50'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\n\nroot.mainloop()<\/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>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"432\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-window-resizable.png\" alt=\"\" class=\"wp-image-1326\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-window-resizable.png 602w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-window-resizable-300x215.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p>When a window is resizable, you can specify the minimum and maximum sizes using the <code>minsize()<\/code> and <code>maxsize()<\/code> methods:<\/p>\n\n\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\">window.minsize(min_width, min_height)\nwindow.maxsize(max_width, max_height)<\/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>\n\n\n<h2 class=\"wp-block-heading\" id='transparency'>Transparency <a href=\"#transparency\" class=\"anchor\" id=\"transparency\" title=\"Anchor for Transparency\">#<\/a><\/h2>\n\n\n\n<p>Tkinter allows you to specify the transparency of a window by setting its alpha channel ranging from 0.0 (fully transparent) to 1.0 (fully opaque):<\/p>\n\n\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\">window.attributes(<span class=\"hljs-string\">'-alpha'<\/span>,<span class=\"hljs-number\">0.5<\/span>)<\/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>\n\n\n<p>The following example illustrates a window with 50% transparent:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" 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\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Window Demo'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'600x400+50+50'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\nroot.attributes(<span class=\"hljs-string\">'-alpha'<\/span>, <span class=\"hljs-number\">0.5<\/span>)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"332\" height=\"259\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-Window-Transparency.png\" alt=\"\" class=\"wp-image-2210\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-Window-Transparency.png 332w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-Window-Transparency-300x234.png 300w\" sizes=\"auto, (max-width: 332px) 100vw, 332px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='window-stacking-order'>Window stacking order <a href=\"#window-stacking-order\" class=\"anchor\" id=\"window-stacking-order\" title=\"Anchor for Window stacking order\">#<\/a><\/h2>\n\n\n\n<p>The window stack order refers to the order of windows placed on the screen from bottom to top. The closer window is on the top of the stack and it overlaps the one lower.<\/p>\n\n\n\n<p>To ensure that a window is always at the top of the stacking order, you can use the <code>-topmost<\/code> attribute like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">window.attributes(<span class=\"hljs-string\">'-topmost'<\/span>, <span class=\"hljs-number\">1<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><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>To move a window up or down of the stack, you can use the <code>lift()<\/code> and <code>lower()<\/code> methods:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">window.lift()\nwindow.lift(another_window)\n\nwindow.lower()\nwindow.lower(another_window)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><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>The following example places the root window on top of all other windows. In other words, the root window is always on top:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" 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\n\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Window Demo'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x200+50+50'<\/span>)\nroot.resizable(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>)\nroot.attributes(<span class=\"hljs-string\">'-topmost'<\/span>, <span class=\"hljs-number\">1<\/span>)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><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<h2 class=\"wp-block-heading\" id='changing-the-default-icon'>Changing the default icon <a href=\"#changing-the-default-icon\" class=\"anchor\" id=\"changing-the-default-icon\" title=\"Anchor for Changing the default icon\">#<\/a><\/h2>\n\n\n\n<p>Tkinter window displays a default icon. To change this default icon, you follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Prepare an image in the <code>.ico<\/code> format. If you have the image in other formats like <code>png<\/code> or <code>jpg<\/code>, you can convert it to the <code>.ico<\/code> format. There are many online tools that allow you to do it quite easily.<\/li>\n\n\n\n<li>Place the icon in a folder that can be accessible from the program.<\/li>\n\n\n\n<li>Call the <code>iconbitmap()<\/code> method of the window object. The <code>iconbitmap()<\/code> method is primarily designed for Windows and require <code>.ico<\/code> files.<\/li>\n<\/ul>\n\n\n\n<p>The following program illustrates how to change the default icon to a new one (on Windows only).<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span>\u00a0tkinter\u00a0<span class=\"hljs-keyword\">as<\/span>\u00a0tk\n\n\nroot\u00a0=\u00a0tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter\u00a0Window\u00a0Demo'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x200+50+50'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>,\u00a0<span class=\"hljs-literal\">False<\/span>)\n<span class=\"hljs-comment\"># Ensure icon is in the assets folder relative to your script.<\/span>\nroot.iconbitmap(<span class=\"hljs-string\">'.\/assets\/pythontutorial.ico'<\/span>)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"302\" height=\"232\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-window-icon.png\" alt=\"Tkinter window - customized icon\" class=\"wp-image-1663\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-window-icon.png 302w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/Tkinter-window-icon-300x230.png 300w\" sizes=\"auto, (max-width: 302px) 100vw, 302px\" \/><\/figure>\n\n\n\n<p>If you want to use the above icon, you can download it to your computer:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/pythontutorial-1.ico\" alt=\"\" class=\"wp-image-1666\" style=\"object-fit:cover;width:64px;height:64px\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/pythontutorial-1.ico 16w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/12\/pythontutorial-1-150x150.ico 150w\" sizes=\"auto, (max-width: 16px) 100vw, 16px\" \/><\/figure>\n\n\n\n<p>On Linux or macOS, you can use a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-photoimage\/\">PhotoImage<\/a> and the <code>iconphoto()<\/code> method to show an icon as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" 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\nroot = tk.Tk()\nroot.title(<span class=\"hljs-string\">'Tkinter Window Demo'<\/span>)\nroot.geometry(<span class=\"hljs-string\">'300x200+50+50'<\/span>)\nroot.resizable(<span class=\"hljs-literal\">False<\/span>, <span class=\"hljs-literal\">False<\/span>)\n\n<span class=\"hljs-keyword\">try<\/span>:\n    <span class=\"hljs-comment\"># Use iconphoto on Linux and macOS<\/span>\n    photo = tk.PhotoImage(file=<span class=\"hljs-string\">'.\/assets\/python_icon.png'<\/span>) <span class=\"hljs-comment\">#.png or .gif<\/span>\n    root.iconphoto(<span class=\"hljs-literal\">False<\/span>, photo)\n<span class=\"hljs-keyword\">except<\/span> tk.TclError:\n    print(<span class=\"hljs-string\">\"icon file not found.\"<\/span>)\n\nroot.mainloop()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><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>Note that you must use a <code>.png<\/code> or <code>.gif<\/code> file instead of an <code>.ico<\/code> file on Linux and macOS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>title()<\/code> method to change the title of the window.<\/li>\n\n\n\n<li>Use the <code>geometry()<\/code> method to change the size and location of the window.<\/li>\n\n\n\n<li>Use the <code>resizable()<\/code> method to specify whether a window can be resizable horizontally or vertically.<\/li>\n\n\n\n<li>Use the <code>window.attributes('-alpha',0.5)<\/code> to set the transparency for the window.<\/li>\n\n\n\n<li>Use the <code>window.attributes('-topmost', 1)<\/code> to make the window always on top.<\/li>\n\n\n\n<li>Use <code>lift()<\/code> and <code>lower()<\/code> methods to move the window up and down of the window stacking order.<\/li>\n\n\n\n<li>Use the <code>iconbitmap()<\/code> method to change the default icon of the window.<\/li>\n<\/ul>\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=\"1321\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/\"\n\t\t\t\tdata-post-title=\"Tkinter Window\"\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=\"1321\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/\"\n\t\t\t\tdata-post-title=\"Tkinter Window\"\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 manipulate various attributes of a Tkinter window.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1321","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1321","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=1321"}],"version-history":[{"count":5,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1321\/revisions"}],"predecessor-version":[{"id":7400,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1321\/revisions\/7400"}],"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=1321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}