{"id":1304,"date":"2020-11-29T02:09:17","date_gmt":"2020-11-29T02:09:17","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=1304"},"modified":"2025-04-04T14:51:42","modified_gmt":"2025-04-04T14:51:42","slug":"tkinter-grid","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-grid\/","title":{"rendered":"Tkinter Grid"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the Tkinter grid geometry manager to position widgets on a window.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-grid-geometry-manager'>Introduction to the Tkinter grid geometry manager <a href=\"#introduction-to-the-tkinter-grid-geometry-manager\" class=\"anchor\" id=\"introduction-to-the-tkinter-grid-geometry-manager\" title=\"Anchor for Introduction to the Tkinter grid geometry manager\">#<\/a><\/h2>\n\n\n\n<p>The grid geometry manager uses the concepts of rows and columns to arrange the <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-ttk\/\">widgets<\/a> on a <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-window\/\">window<\/a> or <a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-frame\/\">frame<\/a>.<\/p>\n\n\n\n<p>The following picture shows a grid that consists of four rows and three columns:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"453\" height=\"349\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Grid-Geometry.png\" alt=\"\" class=\"wp-image-2174\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Grid-Geometry.png 453w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Grid-Geometry-300x231.png 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/figure>\n<\/div>\n\n\n<p>Each row and column in the grid is identified by an index. By default, the first row has an index of zero, the second row has an index of one, and so on. Likewise, the columns in the grid have indexes of zero, one, two, etc.<\/p>\n\n\n\n<p>The indexes of rows and columns in a grid don&#8217;t have to start at zero. Additionally, indexes of the row and column can have gaps. For example, you can have a grid whose column indexes are 1, 2, 10, 11, and 12. The gaps can be useful when you plan to add more widgets in the middle of the grid later.<\/p>\n\n\n\n<p>The intersection of a row and a column is called a <strong>cell<\/strong>. A cell is an area where you can place a widget. A cell can show only one widget at a time. If you place two or more widgets in a cell, they&#8217;ll be on top of each other.<\/p>\n\n\n\n<p>To place multiple widgets in a cell, you use a container like a <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-frame\/\">Frame<\/a><\/code> or <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-labelframe\/\">LabelFrame<\/a><\/code> to wrap the widgets and place container on the cell.<\/p>\n\n\n\n<p>The column&#8217;s width depends on the width of the widget it contains. Similarly, the row&#8217;s height depends on the height of the widgets contained within the row.<\/p>\n\n\n\n<p>Rows and columns can span. The following picture shows a grid with the cell (1,1) that spans two columns and the cell (0,2) that spans two rows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"459\" height=\"350\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-columnspan-rowspan.png\" alt=\"\" class=\"wp-image-2176\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-columnspan-rowspan.png 459w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-columnspan-rowspan-300x229.png 300w\" sizes=\"auto, (max-width: 459px) 100vw, 459px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id='setting-up-the-grid'>Setting up the grid <a href=\"#setting-up-the-grid\" class=\"anchor\" id=\"setting-up-the-grid\" title=\"Anchor for Setting up the grid\">#<\/a><\/h2>\n\n\n\n<p>Before positioning widgets on a grid, you&#8217;ll need to configure the rows and columns of the grid. Tkinter provides you with two methods for configuring grid rows and columns:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">container<\/span><span class=\"hljs-selector-class\">.columnconfigure<\/span>(<span class=\"hljs-selector-tag\">index<\/span>, <span class=\"hljs-selector-tag\">weight<\/span>)\n<span class=\"hljs-selector-tag\">container<\/span><span class=\"hljs-selector-class\">.rowconfigure<\/span>(<span class=\"hljs-selector-tag\">index<\/span>, <span class=\"hljs-selector-tag\">weight<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>columnconfigure()<\/code> method configures the column <code>index<\/code> of a grid. The  <code>weight<\/code> determines how wide the column will occupy, which is relative to other columns. <\/p>\n\n\n\n<p>For example, a column with a weight of 2 will be twice as wide as a column with a weight of 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='positioning-a-widget-on-the-grid'>Positioning a widget on the grid <a href=\"#positioning-a-widget-on-the-grid\" class=\"anchor\" id=\"positioning-a-widget-on-the-grid\" title=\"Anchor for Positioning a widget on the grid\">#<\/a><\/h2>\n\n\n\n<p>To place a widget on the grid, you use the widget&#8217;s <code>grid()<\/code> method:<\/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\">widget.grid(**options)<\/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>The <code>grid()<\/code> method has the following parameters:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Parameters<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td>column<\/td><td>The column index where you want to place the widget.<\/td><\/tr><tr><td>row<\/td><td>The row index where you want to place the widget.<\/td><\/tr><tr><td>rowspan<\/td><td>Set the number of adjacent rows that the widget can span.<\/td><\/tr><tr><td>columnspan<\/td><td>Set the number of adjacent columns that the widget can span.<\/td><\/tr><tr><td>sticky<\/td><td>If the cell is large than the widget, the <code>sticky<\/code> option specifies which side the widget should stick to and how to distribute any extra space within the cell that is not taken up by the widget at its original size.<\/td><\/tr><tr><td>padx<\/td><td>Add external padding above and below the widget.<\/td><\/tr><tr><td>pady<\/td><td>Add external padding to the left and right of the widget.<\/td><\/tr><tr><td>ipadx<\/td><td>Add internal padding inside the widget from the left and right sides.<\/td><\/tr><tr><td>ipady<\/td><td>Add internal padding inside the widget from the top and bottom sides.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='sticky'>Sticky <a href=\"#sticky\" class=\"anchor\" id=\"sticky\" title=\"Anchor for Sticky\">#<\/a><\/h3>\n\n\n\n<p>By default, when a cell is larger than the widget it contains, the grid geometry manager places the widget at the center of the cell horizontally and vertically.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"259\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-Options.png\" alt=\"\" class=\"wp-image-2179\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-Options.png 367w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-Options-300x212.png 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/figure>\n<\/div>\n\n\n<p>To change this default behavior, you can use the <code>sticky<\/code> option. The <code>sticky<\/code> option specifies which edge of the cell the widget should stick to.<\/p>\n\n\n\n<p>The <code>sticky<\/code> has the following valid values:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Sticky<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td>N<\/td><td>North or Top Center<\/td><\/tr><tr><td>S<\/td><td>South or Bottom Center<\/td><\/tr><tr><td>E<\/td><td>East or Right Center<\/td><\/tr><tr><td>W<\/td><td>West or Left Center<\/td><\/tr><tr><td>NW<\/td><td>North West or Top Left<\/td><\/tr><tr><td>NE<\/td><td>North East or Top Right<\/td><\/tr><tr><td>SE<\/td><td>South East or Bottom Right<\/td><\/tr><tr><td>SW<\/td><td>South West or Bottom Left<\/td><\/tr><tr><td>NS<\/td><td>NS stretches the widget vertically. However, it leaves the widget centered horizontally.<\/td><\/tr><tr><td>EW<\/td><td>EW stretches the widget horizontally. However, it leaves the widget centered vertically.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>If you want to position the widget in the corner of the cell, you can use the N, S, E, and W.<\/p>\n\n\n\n<p>The following picture illustrates how to position a widget with the sticky option set to <code>N<\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"259\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-N.png\" alt=\"\" class=\"wp-image-2198\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-N.png 367w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-N-300x212.png 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/figure>\n<\/div>\n\n\n<p>If you want to position the widget centered against one side of the cell, you can use the NW (top left), NE (top right), SE (bottom right), and SW (bottom left).<\/p>\n\n\n\n<p>The following example shows how to position a widget with the sticky option set to <code>NW<\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"366\" height=\"259\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-NW.png\" alt=\"Tkinter grid - Sticky NW\" class=\"wp-image-2197\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-NW.png 366w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-NW-300x212.png 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/figure>\n<\/div>\n\n\n<p>NS stretches the widget vertically. However, it leaves the widget centered horizontally:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"366\" height=\"259\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-NS-1.png\" alt=\"\" class=\"wp-image-2190\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-NS-1.png 366w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-NS-1-300x212.png 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/figure>\n<\/div>\n\n\n<p>EW stretches the widget horizontally. However, it leaves the widget centered vertically:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"366\" height=\"259\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-EW.png\" alt=\"\" class=\"wp-image-2191\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-EW.png 366w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2021\/01\/Tkinter-grid-Sticky-EW-300x212.png 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\" id='padding'>Padding <a href=\"#padding\" class=\"anchor\" id=\"padding\" title=\"Anchor for Padding\">#<\/a><\/h3>\n\n\n\n<p>To add paddings between cells of a grid, you use the <code>padx<\/code> and <code>pady<\/code> options. The <code>padx<\/code> and <code>pady<\/code> are external paddings:<\/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\">grid(column, row, sticky, padx, pady)<\/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>To add paddings within a widget itself, you use <code>ipadx<\/code> and <code>ipady<\/code> options. The <code>ipadx<\/code> and <code>ipady<\/code> are internal paddings:<\/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\">grid(column, row, sticky, padx, pady, ipadx, ipady)<\/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>The internal and external paddings default to zero.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='tkinter-grid-geometry-manager-example'>Tkinter grid geometry manager example <a href=\"#tkinter-grid-geometry-manager-example\" class=\"anchor\" id=\"tkinter-grid-geometry-manager-example\" title=\"Anchor for Tkinter grid geometry manager example\">#<\/a><\/h2>\n\n\n\n<p>In this example, we&#8217;ll use the grid geometry manager to design a login screen as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"242\" height=\"132\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-Grid-Example.png\" alt=\"\" class=\"wp-image-1305\"\/><\/figure>\n<\/div>\n\n\n<p>The login screen uses a grid that has two columns and three rows. Also, the second column is three times as wide as the first column:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"325\" height=\"183\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-Grid-row-and-column-configruation.png\" alt=\"\" class=\"wp-image-1307\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-Grid-row-and-column-configruation.png 325w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2020\/11\/Tkinter-Grid-row-and-column-configruation-300x169.png 300w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><\/figure>\n<\/div>\n\n\n<p>The following shows the complete login window:<\/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\"><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\n<span class=\"hljs-comment\"># main window<\/span>\nroot = tk.Tk()\nroot.geometry(<span class=\"hljs-string\">\"240x100\"<\/span>)\nroot.title(<span class=\"hljs-string\">'Login'<\/span>)\n\n\n<span class=\"hljs-comment\"># grid 3x2<\/span>\nroot.rowconfigure(<span class=\"hljs-number\">0<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\nroot.rowconfigure(<span class=\"hljs-number\">1<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\nroot.rowconfigure(<span class=\"hljs-number\">2<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\nroot.columnconfigure(<span class=\"hljs-number\">0<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\nroot.columnconfigure(<span class=\"hljs-number\">1<\/span>, weight=<span class=\"hljs-number\">3<\/span>)\n\n\n<span class=\"hljs-comment\"># username<\/span>\nusername_label = ttk.Label(root, text=<span class=\"hljs-string\">\"Username:\"<\/span>)\nusername_label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\nusername_entry = ttk.Entry(root)\nusername_entry.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\n<span class=\"hljs-comment\"># password<\/span>\npassword_label = ttk.Label(root, text=<span class=\"hljs-string\">\"Password:\"<\/span>)\npassword_label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">1<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\npassword_entry = ttk.Entry(root,  show=<span class=\"hljs-string\">\"*\"<\/span>)\npassword_entry.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">1<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\n<span class=\"hljs-comment\"># login button<\/span>\nlogin_button = ttk.Button(root, text=<span class=\"hljs-string\">\"Login\"<\/span>)\nlogin_button.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">3<\/span>, sticky=tk.E, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\n\nroot.mainloop()\n<\/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>How it works.<\/p>\n\n\n\n<p>First, use the <code>rowconfigure()<\/code> and  <code>columnconfigure()<\/code> methods to set up a grid of 3 rows 2 columns:<\/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.rowconfigure(<span class=\"hljs-number\">0<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\nroot.rowconfigure(<span class=\"hljs-number\">1<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\nroot.rowconfigure(<span class=\"hljs-number\">2<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\nroot.columnconfigure(<span class=\"hljs-number\">0<\/span>, weight=<span class=\"hljs-number\">1<\/span>)\nroot.columnconfigure(<span class=\"hljs-number\">1<\/span>, weight=<span class=\"hljs-number\">3<\/span>)<\/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>Second, position the username label on the first column and the username entry on the second column of the first row of the grid:<\/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\">username_label = ttk.Label(root, text=<span class=\"hljs-string\">\"Username:\"<\/span>)\nusername_label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\nusername_entry = ttk.Entry(root)\nusername_entry.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">0<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)<\/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>Third, position the password label and entry on the first and second column of the second row:<\/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\">password_label = ttk.Label(root, text=<span class=\"hljs-string\">\"Password:\"<\/span>)\npassword_label.grid(column=<span class=\"hljs-number\">0<\/span>, row=<span class=\"hljs-number\">1<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)\n\npassword_entry = ttk.Entry(root,  show=<span class=\"hljs-string\">\"*\"<\/span>)\npassword_entry.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">1<\/span>, sticky=tk.EW, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/span>)<\/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>Finally, position the login button on the third row. Since its <code>sticky<\/code> option sets to <code>E<\/code>, the button is aligned to the right of the third row.<\/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\">login_button = ttk.Button(root, text=<span class=\"hljs-string\">\"Login\"<\/span>)\nlogin_button.grid(column=<span class=\"hljs-number\">1<\/span>, row=<span class=\"hljs-number\">3<\/span>, sticky=tk.E, padx=<span class=\"hljs-number\">5<\/span>, pady=<span class=\"hljs-number\">5<\/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>\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>columnconfigure()<\/code> and <code>rowconfigure()<\/code> methods to specify the weight of a column and a row of a grid.<\/li>\n\n\n\n<li>Use <code>grid()<\/code> method to position a widget on a grid.<\/li>\n\n\n\n<li>Use <code>sticky<\/code> option to align the position of the widget on a cell and define how the widget will be stretched.<\/li>\n\n\n\n<li>Use <code style=\"background-color: rgb(255, 255, 255); font-size: 16px;\">ipadx<\/code>, <code style=\"background-color: rgb(255, 255, 255); font-size: 16px;\">ipady<\/code> and <code style=\"background-color: rgb(255, 255, 255); font-size: 16px;\">padx<\/code>, <code style=\"background-color: rgb(255, 255, 255); font-size: 16px;\">pady<\/code> to add internal and external paddings.<\/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=\"1304\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-grid\/\"\n\t\t\t\tdata-post-title=\"Tkinter Grid\"\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=\"1304\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-grid\/\"\n\t\t\t\tdata-post-title=\"Tkinter Grid\"\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 grid geometry manager to position widgets on a container such as a frame or a window.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1304","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1304","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=1304"}],"version-history":[{"count":3,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1304\/revisions"}],"predecessor-version":[{"id":7512,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/1304\/revisions\/7512"}],"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=1304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}