{"id":5148,"date":"2020-04-24T17:03:09","date_gmt":"2020-04-24T17:03:09","guid":{"rendered":"https:\/\/www.askpython.com\/?p=5148"},"modified":"2020-04-24T17:03:11","modified_gmt":"2020-04-24T17:03:11","slug":"python-tkinter-grid-example","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/tkinter\/python-tkinter-grid-example","title":{"rendered":"Python &#8211; Tkinter Grid Example"},"content":{"rendered":"\n<p>Hello everyone! In our previous tutorial section on Tkinter, we covered the <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-text-widget-tkinter-scrollbar\" class=\"rank-math-link\">Tkinter text widget<\/a>. Let&#8217;s now look at an example of using the Tkinter Grid manager.<\/p>\n\n\n\n<p>But, you may have a question to ask, especially after seeing a lot of people using the <code>pack<\/code> manager.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why use Tkinter Grid Manager?<\/h2>\n\n\n\n<p>In our previous tutorials, we had been using the <strong>pack <\/strong>geometry manager to manage the geometry of our application. But making it work smoothly with a lot of objects is a difficult task.<\/p>\n\n\n\n<p>Therefore, Tkinter introduced other packing managers to make our life a bit easier, and also have some flexibility on when to use what.<\/p>\n\n\n\n<p>The Tkinter Grid manager is actually the easiest to learn and is the most recommended if you&#8217;re starting out on building Tkinter applications.<\/p>\n\n\n\n<p>Now that we have this covered, let&#8217;s move on to actually using the Grid manager in our application!<\/p>\n\n\n\n<p class=\"has-text-color has-background has-text-align-left has-very-light-gray-color has-vivid-cyan-blue-background-color\"><strong>NOTE<\/strong>: Never use multiple packing managers in the same Tkinter application. This will cause unintended bugs, and is not recommended at all. Use only one packing manager for a single application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Tkinter Grid Geometry Manager<\/h2>\n\n\n\n<p>Let&#8217;s design the below layout using the Grid manager.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"316\" height=\"136\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/layout.png\" alt=\"Layout\" class=\"wp-image-5151\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/layout.png 316w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/layout-300x129.png 300w\" sizes=\"auto, (max-width: 316px) 100vw, 316px\" \/><figcaption>Layout<\/figcaption><\/figure><\/div>\n\n\n\n<p>This layout will have two <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-entry-widget\" class=\"rank-math-link\">entry widgets<\/a>, with a label for each, and a button widget below.<\/p>\n\n\n\n<p>We will also add an image to the right, and a button widget for the image too.<\/p>\n\n\n\n<p>While this type of layout is hard to manage using <code>pack<\/code>, we can easily make this using <code>grid<\/code>!<\/p>\n\n\n\n<p>The steps are simple enough. We just need to create all the widgets we need, and tell the <code>grid<\/code> manager how to place them.<\/p>\n\n\n\n<p>We&#8217;ll first create our master object.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport tkinter as tk\n\nmaster = tk.Tk()\n<\/pre><\/div>\n\n\n<p>Now, let&#8217;s create two labels first, since we need them on the left most side, and tell the <code>grid<\/code> manager to place it on the respective row number.<\/p>\n\n\n\n<p>We need the labels at column number 0, indexed by row numbers 0 and 1. After creating the labels, we can directly pack them using <code>grid<\/code> by using:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nlabel_object.grid(row, col)\n<\/pre><\/div>\n\n\n<p>So, we can directly write it as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntk.Label(master, text=&quot;Label 1&quot;).grid(row=0, column=0)\ntk.Label(master, text=&quot;Label 2&quot;).grid(row=1, column=0)\n<\/pre><\/div>\n\n\n<p>Let&#8217;s now add an entry for each of the two labels.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ne1 = tk.Entry(master)\ne2 = tk.Entry(master)\n<\/pre><\/div>\n\n\n<p>We have created the entry objects, but now, we need to tell <code>grid<\/code> to place them in their respective position.<\/p>\n\n\n\n<p>Simply call <code>entry_obj.grid()<\/code>! This is similar to pack, but overall, is much more smoother to use.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ne1.grid(row=0, column=1)\ne2.grid(row=1, column=1)\n<\/pre><\/div>\n\n\n<p>After this, we can add our tkinter main-loop using <code>tk.mainloop()<\/code>.<\/p>\n\n\n\n<p>I&#8217;ll post the complete code until this point.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport tkinter as tk\n\n# Create the master object\nmaster = tk.Tk()\n\n# Create the label objects and pack them using grid\ntk.Label(master, text=&quot;Label 1&quot;).grid(row=0, column=0)\ntk.Label(master, text=&quot;Label 2&quot;).grid(row=1, column=0)\n\n# Create the entry objects using master\ne1 = tk.Entry(master)\ne2 = tk.Entry(master)\n\n# Pack them using grid\ne1.grid(row=0, column=1)\ne2.grid(row=1, column=1)\n\n# The mainloop\ntk.mainloop()\n\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"339\" height=\"144\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/tkinter_grid_sample.png\" alt=\"Tkinter Grid Sample\" class=\"wp-image-5154\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/tkinter_grid_sample.png 339w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/tkinter_grid_sample-300x127.png 300w\" sizes=\"auto, (max-width: 339px) 100vw, 339px\" \/><figcaption>Tkinter Grid Sample<\/figcaption><\/figure><\/div>\n\n\n\n<p>Alright! This seems to work as expected. Now, let&#8217;s add a button to it, right below!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nbutton1 = tk.Button(master, text=&quot;Button 1&quot;)\nbutton1.grid(columnspan=2, row=2, column=0)\n<\/pre><\/div>\n\n\n<p>Now, we have our left side covered.<\/p>\n\n\n\n<p>Let&#8217;s add the image and another button to the right side.<\/p>\n\n\n\n<p>As we discussed the issues of displaying an image on our <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-canvas\" class=\"rank-math-link\">previous tutorial<\/a>, we must hold a reference to the <strong>PhotoImage <\/strong>object to avoid automatic garbage collection!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom PIL import Image, ImageTk\n\n# Create the PIL image object\nimage = Image.open(&quot;debian.png&quot;)\nphoto = ImageTk.PhotoImage(image)\n\n# Create an image label\nimg_label = tk.Label(image=photo)\n# Store a reference to a PhotoImage object, to avoid it\n# being garbage collected! This is necesary to display the image!\nimg_label.image = photo\n\nimg_label.grid(row=0, column=2)\n<\/pre><\/div>\n\n\n<p>Finally, let&#8217;s add a button at the bottom.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Create another button\nbutton2 = tk.Button(master, text=&quot;Button 2&quot;)\nbutton2.grid(columnspan=2, row=2, column=2)\n<\/pre><\/div>\n\n\n<p>Now, I&#8217;ll post the complete program here.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport tkinter as tk\nfrom PIL import Image, ImageTk\n\n# Create the master object\nmaster = tk.Tk()\n\n# Create the label objects and pack them using grid\ntk.Label(master, text=&quot;Label 1&quot;).grid(row=0, column=0)\ntk.Label(master, text=&quot;Label 2&quot;).grid(row=1, column=0)\n\n# Create the entry objects using master\ne1 = tk.Entry(master)\ne2 = tk.Entry(master)\n\n# Pack them using grid\ne1.grid(row=0, column=1)\ne2.grid(row=1, column=1)\n\nbutton1 = tk.Button(master, text=&quot;Button 1&quot;)\nbutton1.grid(columnspan=2, row=2, column=0)\n\n# Create the PIL image object\nimage = Image.open(&quot;debian.png&quot;)\nphoto = ImageTk.PhotoImage(image)\n\n# Create an image label\nimg_label = tk.Label(image=photo)\n# Store a reference to a PhotoImage object, to avoid it\n# being garbage collected! This is necesary to display the image!\nimg_label.image = photo\n\nimg_label.grid(row=0, column=2)\n\n# Create another button\nbutton2 = tk.Button(master, text=&quot;Button 2&quot;)\nbutton2.grid(columnspan=2, row=2, column=2)\n\n# The mainloop\ntk.mainloop()\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"601\" height=\"423\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/tkinter_grid_complete_layout.png\" alt=\"Tkinter Grid Complete Layout\" class=\"wp-image-5156\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/tkinter_grid_complete_layout.png 601w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/04\/tkinter_grid_complete_layout-300x211.png 300w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption>Tkinter Grid Complete Layout<\/figcaption><\/figure><\/div>\n\n\n\n<p>Finally, we&#8217;ve completed our layout! And it was as simple as just creating the widgets and telling <code>grid<\/code> to place them in their correct positions!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we learned how we could add widgets to our Tkinter application and design layouts using the Tkinter Grid Geometry Manager.<\/p>\n\n\n\n<p>Stay tuned for more Tkinter content!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone! In our previous tutorial section on Tkinter, we covered the Tkinter text widget. Let&#8217;s now look at an example of using the Tkinter Grid manager. But, you may have a question to ask, especially after seeing a lot of people using the pack manager. Why use Tkinter Grid Manager? In our previous tutorials, [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":5158,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"class_list":["post-5148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tkinter"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=5148"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/5148\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/5158"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=5148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=5148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=5148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}