{"id":26647,"date":"2022-01-29T16:43:42","date_gmt":"2022-01-29T16:43:42","guid":{"rendered":"https:\/\/www.askpython.com\/?p=26647"},"modified":"2022-01-29T16:43:42","modified_gmt":"2022-01-29T16:43:42","slug":"draw-shapes","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/tkinter\/draw-shapes","title":{"rendered":"How to Draw Different Shapes Using Tkinter"},"content":{"rendered":"\n<p>In this tutorial, we\u2019ll learn how to draw different shapes in Tkinter such as square, circle, rectangle, oval, arc, etc. In Tkinter, the <strong>canvas class <\/strong>is used to draw different shapes in our application window. Functions to draw shapes are defined in canvas class so we need to define the canvas class first.<\/p>\n\n\n\n<p>Follow the below step-by-step tutorial to draw different shapes in Python GUI using Tkinter.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/drawing-lines\" data-type=\"post\" data-id=\"26655\">Drawing Lines using Tkinter \u2013 Basics for Beginners<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"set-up-tkinter\">Set up Tkinter<\/h2>\n\n\n\n<p>To install the Tkinter module run the below command in the terminal.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npip install tk\n<\/pre><\/div>\n\n\n<p>To import the installed module, we import all the methods in the Tkinter library by using *:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom tkinter import *\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"drawing-shapes-in-python-tkinter-canvas\">Drawing Shapes in Python Tkinter Canvas<\/h2>\n\n\n\n<p>First, we need to <strong>initialize <\/strong>the Tkinter and canvas class. <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-canvas\" data-type=\"post\" data-id=\"4828\">Tkinter Canvas<\/a> class contains every method for creating different shapes.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nroot = Tk()\ncanvas = Canvas()\nroot.mainloop()\n<\/pre><\/div>\n\n\n<p>After initialization of the Tkinter and canvas class, we start with the drawing of different shapes<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-oval\">1. Oval<\/h3>\n\n\n\n<p>Oval can be easily drawn using the <strong>create_oval()<\/strong> method. This method takes coordinates, color, outline, width, etc. as a parameter. All shapes are created inside a box whose coordinates we provide.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncreate_oval(x0, y0, x1, y1)\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"500\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/circle-inside-square-in-tkinter.jpg\" alt=\"Circle Inside Square In Tkinter\" class=\"wp-image-26673\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/circle-inside-square-in-tkinter.jpg 500w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/circle-inside-square-in-tkinter-300x300.jpg 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/circle-inside-square-in-tkinter-150x150.jpg 150w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><figcaption>Circle Inside Square In Tkinter<\/figcaption><\/figure><\/div>\n\n\n\n<p>In the above code, we\u2019ve set the coordinates of the box so it makes a perfect square and inside that square, we\u2019ll have our perfect circle.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncanvas.create_oval(10, 10, 80, 80, outline = &quot;black&quot;, fill = &quot;white&quot;,width = 2)\ncanvas.pack()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"621\" height=\"290\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-oval-in-tkinter.jpg\" alt=\"Draw Oval In Tkinter\" class=\"wp-image-26672\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-oval-in-tkinter.jpg 621w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-oval-in-tkinter-300x140.jpg 300w\" sizes=\"auto, (max-width: 621px) 100vw, 621px\" \/><figcaption>Draw Oval In Tkinter<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"circlethere-is-no-special-function-for-creating-a-circle-it-is-drawn-by-using-create-oval-function-we-just-need-to-keep-the-horizontal-length-the-same-as-the-vertical-length\">2. Circle<\/h3>\n\n\n\n<p id=\"circlethere-is-no-special-function-for-creating-a-circle-it-is-drawn-by-using-create-oval-function-we-just-need-to-keep-the-horizontal-length-the-same-as-the-vertical-length\">There is no special function for creating a circle, it is drawn by using the create_oval() function. We just need to keep the horizontal length the same as the vertical length. Mathematically, meaning the radius of the shape(circle) is equal to all sides of the boundary(diameter).<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncanvas.create_oval(110,10,210,110,outline = &quot;black&quot;,fill = &quot;white&quot;,width = 2)\ncanvas.pack()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"610\" height=\"255\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-cicle-in-tkinter.jpg\" alt=\"Draw Cicle In Tkinter\" class=\"wp-image-26671\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-cicle-in-tkinter.jpg 610w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-cicle-in-tkinter-300x125.jpg 300w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><figcaption>Draw Cicle In Tkinter<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-square-or-rectangle\">3. Square or Rectangle<\/h3>\n\n\n\n<p>By using the create_rectangle method we draw a rectangle and square shapes. Here we pass the edges\/sides of our shape and hence can draw a square also, using the same method (all sides equal).<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncanvas.create_rectangle(10,10,110,110,outline =&quot;black&quot;,fill =&quot;white&quot;,width = 2)\ncanvas.create_rectangle(210,10,310,210,outline =&quot;black&quot;,fill =&quot;white&quot;,width =2)\ncanvas.pack()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"623\" height=\"382\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-square-or-rectangle-in-tkinter.jpg\" alt=\"Draw Square Or Rectangle In Tkinter\" class=\"wp-image-26670\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-square-or-rectangle-in-tkinter.jpg 623w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-square-or-rectangle-in-tkinter-300x184.jpg 300w\" sizes=\"auto, (max-width: 623px) 100vw, 623px\" \/><figcaption>Draw Square Or Rectangle In Tkinter<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-polygon\">4. Polygon<\/h3>\n\n\n\n<p>We can draw as many vertices as we want. We use the <strong>create_polygon() <\/strong>method which takes coordinates of edges and renders them accordingly on our main window. In the below code we\u2019ve created a list of coordinates and passed it into our create_polygon method.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#points for the vertices in x1,y1,x2,y2, and so on\n\npoints = &#x5B;150, 100, 200, 120, 240, 180, 210, 200, 150, 150, 100, 200]\n\ncanvas.create_polygon(points, outline = &quot;blue&quot;, fill = &quot;orange&quot;, width = 2)\ncanvas.pack()\n<\/pre><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"605\" height=\"340\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-polygon-using-tkinter.jpg\" alt=\"Draw Polygon Using Tkinter\" class=\"wp-image-26669\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-polygon-using-tkinter.jpg 605w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-polygon-using-tkinter-300x169.jpg 300w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><figcaption>Draw Polygon Using Tkinter<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-arc\">5. Arc<\/h3>\n\n\n\n<p>We create arc shapes by using <strong>create_arc <\/strong>method.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>canvas.create_arc(30,200,90,100,extent =210,outline =&#8221;black&#8221;,fill =&#8221;white&#8221;, width =2)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"501\" height=\"341\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-arc-using-tkinter.jpg\" alt=\"Draw Arc Using Tkinter\" class=\"wp-image-26668\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-arc-using-tkinter.jpg 501w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/01\/draw-arc-using-tkinter-300x204.jpg 300w\" sizes=\"auto, (max-width: 501px) 100vw, 501px\" \/><figcaption>Draw Arc Using Tkinter<\/figcaption><\/figure><\/div>\n\n\n\n<p>Different Parameters Used In Create Methods<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Outline :- <\/strong>outline is used to define the color of the shape\u2019s outline.<\/li><li><strong>Fill :- <\/strong>fill is like a paint bucket tool in microsoft paint. It fills the shape with color we\u2019ve assigned to it.<\/li><li><strong>Width :- <\/strong>used to set the width of the outline.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>That&#8217;s it for this basic tutorial on creating shapes using Tkinter. Go ahead and explore the functions that help you draw different shapes and lines in <a href=\"https:\/\/www.askpython.com\/python-modules\/tkinter\/tkinter-font-class\" data-type=\"post\" data-id=\"22690\">Python Tkinter<\/a> to create even more advanced shapes and patterns. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we\u2019ll learn how to draw different shapes in Tkinter such as square, circle, rectangle, oval, arc, etc. In Tkinter, the canvas class is used to draw different shapes in our application window. Functions to draw shapes are defined in canvas class so we need to define the canvas class first. Follow the [&hellip;]<\/p>\n","protected":false},"author":38,"featured_media":26657,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92],"tags":[],"class_list":["post-26647","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\/26647","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\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=26647"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/26647\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/26657"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=26647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=26647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=26647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}