{"id":4595,"date":"2022-09-08T01:45:14","date_gmt":"2022-09-08T01:45:14","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4595"},"modified":"2022-09-08T01:47:46","modified_gmt":"2022-09-08T01:47:46","slug":"tkinter-canvas","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-canvas\/","title":{"rendered":"Tkinter Canvas"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Tkinter Canvas widget and how to draw various objects on it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-tkinter-canvas-widget'>Introduction to the Tkinter canvas widget <a href=\"#introduction-to-the-tkinter-canvas-widget\" class=\"anchor\" id=\"introduction-to-the-tkinter-canvas-widget\" title=\"Anchor for Introduction to the Tkinter canvas widget\">#<\/a><\/h2>\n\n\n\n<p>The canvas widget is the most flexible widget in Tkinter. The Canvas widget allows you to build anything from custom widgets to complete user interfaces. <\/p>\n\n\n\n<p>The canvas widget is a blank area on which you can draw figures, create text, and place images. <\/p>\n\n\n\n<p>To create a canvas widget, you create a new instance of the <code>Canvas<\/code> class from the <code>tkinter<\/code> module. For example, the following creates a canvas on a 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\r\n\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'800x600'<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Canvas Demo'<\/span>)\r\n\r\ncanvas = tk.Canvas(root, width=<span class=\"hljs-number\">600<\/span>, height=<span class=\"hljs-number\">400<\/span>, bg=<span class=\"hljs-string\">'white'<\/span>)\r\ncanvas.pack(anchor=tk.CENTER, expand=<span class=\"hljs-literal\">True<\/span>)\r\n\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>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"802\" height=\"646\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-demo.png\" alt=\"\" class=\"wp-image-4632\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-demo.png 802w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-demo-300x242.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-demo-768x619.png 768w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/figure>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, create a new <code>Canvas<\/code> object with the width <code>600px<\/code>, height <code>400px<\/code> and background <code>white<\/code>:<\/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\">canvas = tk.Canvas(root, width=<span class=\"hljs-number\">600<\/span>, height=<span class=\"hljs-number\">400<\/span>, bg=<span class=\"hljs-string\">'white'<\/span>)<\/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>Second, place the <code>canvas<\/code> object on the <code>root<\/code> window using the <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-pack\/\">pack()<\/a><\/code> geometry.<\/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\">canvas.pack(anchor=tk.CENTER, expand=<span class=\"hljs-literal\">True<\/span>)<\/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>A canvas has a coordinate system like a window. The origin <code>(0,0)<\/code> is at the top-left corner. The direction of the x-axis is from left to right and the direction of the y-axis is from top to bottom.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-items-to-a-canvas-using-create_-methods'>Adding items to a canvas using create_* methods <a href=\"#adding-items-to-a-canvas-using-create_-methods\" class=\"anchor\" id=\"adding-items-to-a-canvas-using-create_-methods\" title=\"Anchor for Adding items to a canvas using create_* methods\">#<\/a><\/h2>\n\n\n\n<p>A canvas object has a number of <code>add_*<\/code> methods. These methods allow you to place items on it. The items are:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Item<\/th><th>Method<\/th><\/tr><\/thead><tbody><tr><td>Line<\/td><td>create_line()<\/td><\/tr><tr><td>Rectangle<\/td><td>create_rectangle()<\/td><\/tr><tr><td>Oval<\/td><td>create_oval()<\/td><\/tr><tr><td>Arc<\/td><td>create_arc()<\/td><\/tr><tr><td>Polygon<\/td><td>create_polygon()<\/td><\/tr><tr><td>Text<\/td><td>create_text(()<\/td><\/tr><tr><td>Image<\/td><td>create_image()<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='creating-a-line'>Creating a line <a href=\"#creating-a-line\" class=\"anchor\" id=\"creating-a-line\" title=\"Anchor for Creating a line\">#<\/a><\/h3>\n\n\n\n<p>To create a line, you use the <code>create_line()<\/code> method. For example, the following creates a red line:<\/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\">canvas.create_line((<span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">50<\/span>), (<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>), width=<span class=\"hljs-number\">4<\/span>, fill=<span class=\"hljs-string\">'red'<\/span>)<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"799\" height=\"640\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-create_line.png\" alt=\"\" class=\"wp-image-4633\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-create_line.png 799w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-create_line-300x240.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-create_line-768x615.png 768w\" sizes=\"auto, (max-width: 799px) 100vw, 799px\" \/><\/figure>\n\n\n\n<p>In this example, a line consists of two points <code>(50,50)<\/code> and <code>(100,100)<\/code>. The <code>create_line()<\/code> method connects the dots between these points.<\/p>\n\n\n\n<p>The <code>width<\/code> argument specifies the width of the line. And the <code>fill<\/code> argument specifies the color of the line.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='creating-a-rectangle'>Creating a rectangle <a href=\"#creating-a-rectangle\" class=\"anchor\" id=\"creating-a-rectangle\" title=\"Anchor for Creating a rectangle\">#<\/a><\/h3>\n\n\n\n<p>To draw a rectangle, you use the <code>create_rectangle()<\/code> method. For example:<\/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\r\n\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'800x600'<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Canvas Demo - Rectangle'<\/span>)\r\n\r\ncanvas = tk.Canvas(root, width=<span class=\"hljs-number\">600<\/span>, height=<span class=\"hljs-number\">400<\/span>, bg=<span class=\"hljs-string\">'white'<\/span>)\r\ncanvas.pack(anchor=tk.CENTER, expand=<span class=\"hljs-literal\">True<\/span>)\r\n\r\ncanvas.create_rectangle((<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>), (<span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">300<\/span>), fill=<span class=\"hljs-string\">'green'<\/span>)\r\n\r\nroot.mainloop()<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"644\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-rectangle.png\" alt=\"\" class=\"wp-image-4634\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-rectangle.png 800w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-rectangle-300x242.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-rectangle-768x618.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='creating-an-oval'>Creating an oval <a href=\"#creating-an-oval\" class=\"anchor\" id=\"creating-an-oval\" title=\"Anchor for Creating an oval\">#<\/a><\/h3>\n\n\n\n<p>To draw an oval, you use the <code>create_oval()<\/code> method. For example:<\/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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'800x600'<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Canvas Demo - Oval'<\/span>)\r\n\r\ncanvas = tk.Canvas(root, width=<span class=\"hljs-number\">600<\/span>, height=<span class=\"hljs-number\">400<\/span>, bg=<span class=\"hljs-string\">'white'<\/span>)\r\ncanvas.pack(anchor=tk.CENTER, expand=<span class=\"hljs-literal\">True<\/span>)\r\n\r\n\rpoints = (\r\n    (<span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">150<\/span>),\r\n    (<span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">350<\/span>),\r\n)\r\ncanvas.create_oval(*points, fill=<span class=\"hljs-string\">'purple'<\/span>)\r\n\r\nroot.mainloop()<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"644\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-oval.png\" alt=\"\" class=\"wp-image-4635\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-oval.png 800w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-oval-300x242.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-oval-768x618.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>Like a rectangle, an oval takes the coordinate of the upper-left and lower-right corners of its <em>bounding box<\/em>. A bounding box of an oval is the smallest rectangle that contains the oval.<\/p>\n\n\n\n<p>In this example, the upper-left and lower-right corners of the bounding box are <code>(50,150)<\/code> and <code>(200,350)<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"644\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-bounding-box.png\" alt=\"\" class=\"wp-image-4636\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-bounding-box.png 800w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-bounding-box-300x242.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-bounding-box-768x618.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='creating-a-polygon'>Creating a polygon <a href=\"#creating-a-polygon\" class=\"anchor\" id=\"creating-a-polygon\" title=\"Anchor for Creating a polygon\">#<\/a><\/h3>\n\n\n\n<p>To draw a polygon, you use the <code>create_polygon()<\/code> method. For example:<\/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\r\n\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'800x600'<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Canvas Demo - Polygon'<\/span>)\r\n\r\ncanvas = tk.Canvas(root, width=<span class=\"hljs-number\">600<\/span>, height=<span class=\"hljs-number\">400<\/span>, bg=<span class=\"hljs-string\">'white'<\/span>)\r\ncanvas.pack(anchor=tk.CENTER, expand=<span class=\"hljs-literal\">True<\/span>)\r\n\r\n\r\n<span class=\"hljs-comment\"># create a polygon<\/span>\r\npoints = (\r\n    (<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">300<\/span>),\r\n    (<span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">200<\/span>),\r\n    (<span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">300<\/span>),\r\n)\r\ncanvas.create_polygon(*points, fill=<span class=\"hljs-string\">'blue'<\/span>)\r\n\r\nroot.mainloop()\r\n<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"644\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-create_polygon.png\" alt=\"\" class=\"wp-image-4637\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-create_polygon.png 800w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-create_polygon-300x242.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-create_polygon-768x618.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='creating-a-text'>Creating a text <a href=\"#creating-a-text\" class=\"anchor\" id=\"creating-a-text\" title=\"Anchor for Creating a text\">#<\/a><\/h3>\n\n\n\n<p>To place a text on a canvas, you use the <code>create_text()<\/code> method. For example:<\/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\r\n\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'800x600'<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Canvas Demo - Text'<\/span>)\r\n\r\ncanvas = tk.Canvas(root, width=<span class=\"hljs-number\">600<\/span>, height=<span class=\"hljs-number\">400<\/span>, bg=<span class=\"hljs-string\">'white'<\/span>)\r\ncanvas.pack(anchor=tk.CENTER, expand=<span class=\"hljs-literal\">True<\/span>)\r\n\r\n\r\ncanvas.create_text(\r\n    (<span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">100<\/span>),\r\n    text=<span class=\"hljs-string\">\"Canvas Demo\"<\/span>,\r\n    fill=<span class=\"hljs-string\">\"orange\"<\/span>,\r\n    font=<span class=\"hljs-string\">'tkDefaeultFont 24'<\/span>\r\n)\r\n\r\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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"644\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-text.png\" alt=\"\" class=\"wp-image-4638\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-text.png 800w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-text-300x242.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-text-768x618.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='create-an-arc'>Create an arc <a href=\"#create-an-arc\" class=\"anchor\" id=\"create-an-arc\" title=\"Anchor for Create an arc\">#<\/a><\/h3>\n\n\n\n<p>To draw an arc on a canvas, you use the <code>create_arc()<\/code> method. For example:<\/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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n<span class=\"hljs-keyword\">from<\/span> turtle <span class=\"hljs-keyword\">import<\/span> width\r\n\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'800x600'<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Canvas Demo - Arc'<\/span>)\r\n\r\ncanvas = tk.Canvas(root, width=<span class=\"hljs-number\">600<\/span>, height=<span class=\"hljs-number\">600<\/span>, bg=<span class=\"hljs-string\">'white'<\/span>)\r\ncanvas.pack(anchor=tk.CENTER, expand=<span class=\"hljs-literal\">True<\/span>)\r\n\r\ncanvas.create_arc((<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">10<\/span>), (<span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">200<\/span>), style=tk.PIESLICE, width=<span class=\"hljs-number\">2<\/span>)\r\ncanvas.create_arc((<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">200<\/span>), (<span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">390<\/span>), style=tk.CHORD, width=<span class=\"hljs-number\">2<\/span>)\r\ncanvas.create_arc((<span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">400<\/span>), (<span class=\"hljs-number\">200<\/span>, <span class=\"hljs-number\">590<\/span>), style=tk.ARC, width=<span class=\"hljs-number\">2<\/span>)\r\n\r\n\r\nroot.mainloop()\n<\/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<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"644\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-arc.png\" alt=\"\" class=\"wp-image-4639\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-arc.png 800w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-arc-300x242.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-arc-768x618.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id='create-an-image'>Create an image <a href=\"#create-an-image\" class=\"anchor\" id=\"create-an-image\" title=\"Anchor for Create an image\">#<\/a><\/h3>\n\n\n\n<p>To place an image on a canvas, you use the <code>create_image()<\/code> method. For example:<\/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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'800x600'<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Canvas Demo - Image'<\/span>)\r\n\r\ncanvas = tk.Canvas(root, width=<span class=\"hljs-number\">600<\/span>, height=<span class=\"hljs-number\">400<\/span>, bg=<span class=\"hljs-string\">'white'<\/span>)\r\ncanvas.pack(anchor=tk.CENTER, expand=<span class=\"hljs-literal\">True<\/span>)\r\n\r\npython_image = tk.PhotoImage(file=<span class=\"hljs-string\">'python.gif'<\/span>)\r\ncanvas.create_image(\r\n    (<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>),\r\n    image=python_image\r\n)\r\n\r\n\rroot.mainloop()<\/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>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"644\" src=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-image.png\" alt=\"\" class=\"wp-image-4640\" srcset=\"https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-image.png 800w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-image-300x242.png 300w, https:\/\/www.pythontutorial.net\/wp-content\/uploads\/2022\/09\/tkinter-canvas-image-768x618.png 768w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/figure>\n\n\n\n<p>Note that if you pass directly the <code><a href=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-photoimage\/\">PhotoImage<\/a><\/code> to the <code>create_image()<\/code> method, the image won&#8217;t display because it is automatically <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-garbage-collection\/\">garbage collected<\/a>.<\/p>\n\n\n\n<p>The following code won&#8217;t work:<\/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\">canvas.create_image(\r\n    (<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>),\r\n    image=tk.PhotoImage(file=<span class=\"hljs-string\">'python.gif'<\/span>)\r\n)<\/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<h2 class=\"wp-block-heading\" id='binding-an-event-to-the-item'>Binding an event to the item <a href=\"#binding-an-event-to-the-item\" class=\"anchor\" id=\"binding-an-event-to-the-item\" title=\"Anchor for Binding an event to the item\">#<\/a><\/h2>\n\n\n\n<p>All the <code>create_*<\/code> method returns a string value that identifies the item in the context of the <code>Canvas<\/code> object. And you can use this value to bind an event to the item. <\/p>\n\n\n\n<p>To bind an event to an item, you use the <code>tag_bind()<\/code> method of the <code>Canvas<\/code> object. For example:<\/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\"><span class=\"hljs-keyword\">import<\/span> tkinter <span class=\"hljs-keyword\">as<\/span> tk\r\n\r\nroot = tk.Tk()\r\nroot.geometry(<span class=\"hljs-string\">'800x600'<\/span>)\r\nroot.title(<span class=\"hljs-string\">'Canvas Demo - Binding Event'<\/span>)\r\n\r\ncanvas = tk.Canvas(root, width=<span class=\"hljs-number\">600<\/span>, height=<span class=\"hljs-number\">400<\/span>, bg=<span class=\"hljs-string\">'white'<\/span>)\r\ncanvas.pack(anchor=tk.CENTER, expand=<span class=\"hljs-literal\">True<\/span>)\r\n\r\npython_image = tk.PhotoImage(file=<span class=\"hljs-string\">'python.gif'<\/span>)\r\nimage_item = canvas.create_image(\r\n    (<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">100<\/span>),\r\n    image=python_image\r\n)\r\ncanvas.tag_bind(\r\n    image_item,\r\n    <span class=\"hljs-string\">'&lt;Button-1&gt;'<\/span>,\r\n    <span class=\"hljs-keyword\">lambda<\/span> e: canvas.delete(image_item)\r\n)\r\n\r\nroot.mainloop()<\/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<p>In this example, we bind the left-mouse click to the image item. If you click the image, the <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-lambda-expressions\/\">lambda<\/a> will execute that removes the image from the canvas.<\/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\"><li>A canvas is a blank area where you can draw items such as lines, rectangles, ovals, arcs, texts, and images.<\/li><li>Use <code>Canvas()<\/code> to create a new canvas object.<\/li><li>Use <code>tag_bind()<\/code> method to bind an event to an item on a canvas.<\/li><\/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=\"4595\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-canvas\/\"\n\t\t\t\tdata-post-title=\"Tkinter Canvas\"\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=\"4595\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/tkinter\/tkinter-canvas\/\"\n\t\t\t\tdata-post-title=\"Tkinter Canvas\"\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 about the Tkinter Canvas widget and how to draw various objects on it.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":29,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4595","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4595","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=4595"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4595\/revisions"}],"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=4595"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}