Python Tkinter Canvas - Draw shapes in Python

Last updated Dec 20, 2021

The TKinter Canvas widget helps us display numerous visuals on the python program. It is used to design basic shapes to intricate graphs.  Using Tkinter canvas, we can also show different sorts of custom widgets as per our needs.


The general syntax to Tkinter canvas is:

C = Canvas( root, options)


Where Root represents the parent window, and the options include the popularly used options for the widget.  The following  are some of the popular options that you  can use   when drawing  canvas:

  • Bg -  this option is used when inserting the background color of an object.

  • Bd -  this option represents the  border in pixels

  • Confine -  this option enables  the canvas  not to be  scrolled when the mouse cursor  is  outside the region

  • Height-  we use this option to insert the value of the canvas height (Y dimension).

  • Relief- we use this option to select the type of border that we would like to use in our canvas.  The border can be  RIDGE, GROOVE, RAISED, and SUNKEN.

  • Scrollregion-  it defines the region that can be scrolled.  Usually, it is represented in four letters s,e,n, and w, where w  represents the left side, n represents the top of the widget,  e represents the right side, and represents the bottom.

  • Width- This value provides the value  for the  width of the canvas (X dimension)

  • Xscrollincrement – used to control the horizontal movement of the drawn canvas.

  • Yscrollincrement -  used to control the verticle movement of the drawn canvas.

In addition to the above-listed options, you can also use the following  canvas  options to draw   various objects:

  • Arc – with this option, you can simply draw an arc-shaped object, a cord, or just a simple arc.

  • Image-  this option  allows  you  to add an   image to your canvas

  • Line- this option  is used  to drawing lines in a canvas

  • Oval- this is the option for creating the ellipse and circular objects. When drawing, this option takes two pairs at the bottom and top, bounding the rectangle for the oval.

  • Polygon- this option creates an item that has more than three vertices. 


Example of a Tkinter Canvas

import Tkinter

top = Tkinter.Tk()

C = Tkinter.Canvas(top, bg="red", height=400, width=430)

coord = 20, 60, 300, 310

arc = C.create_arc(coord, start=0, extent=150, fill="red")





from tkinter import *

root = Tk()

root.title("Draw your Pic ")


def paint(event):

    # Co-ordinates.

    x1, y1, x2, y2 = (event.x - 3), (event.y - 3), (event.x + 3), (event.y + 3)

    Colour = "#000fff000"

    w.create_line(x1, y1, x2,

                  y2, fill=Colour)

w = Canvas(root, width=400, height=250)

w.bind("<B1-Motion>", paint)

l = Label(root, text="Double Click and Drag to draw.")






Python Tkinter Canvas example


Article Contributed By :