We now have a youtube channel. Subscribe!

Python Tkinter Canvas Widget

Hello dear readers! welcome back to another section of our tutorial on Python. In this tutorial post, we are going to be discussing about Python Tkinter Canvas Widget.

Canvas is a rectangular region that is intended for drawing images or other complex layouts. You can place text, graphics, widgets, or even frames on a canvas.


The following below is the syntax for create this widget -

w = Canvas ( master, option=value, ... )

Parameter Details

  • master - This represents the parent window.
  • options - Following below is the list of commonly used options for this widget. These options can be used as key-value pairs that are separated by commas.

Sr.No.Option & Description
Border width in pixels. Default is 2.
Normal background color.
If true (the default), the canvas cannot be scrolled outside of the scrollregion.
Cursor used in the canvas like arrow, circle, dot etc.
Size of the canvas in the Y dimension.
Color shown in the focus highlight.
Relief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE.
A tuple (w, n, e, s) that defines over how large an area the canvas can be scrolled, where w is the left side, n the top, e the right side, and s the bottom.
Size of the canvas in the X dimension.
If you set this option to some positive dimension, the canvas can be positioned only on multiples of that distance, and the value will be used for scrolling by scrolling units, such as when the user clicks on the arrows at the ends of a scrollbar.
If the canvas is scrollable, this attribute should be the .set() method of the horizontal scrollbar.
Works like xscrollincrement, but governs vertical movement.
If the canvas is scrollable, this attribute should be the .set() method of the vertical scrollbar.

The canvas widget can support the following standard items -

arc - Creates an arc item, which can be a chord, a pieslice or a simple arc.

coord = 10, 50, 240, 210
arc = canvas.create_arc(coord, start=0, extent=150, fill="blue")

image - This creates an image item, which can be an instance of either the BitmapImage or the PhotoImage classes.

filename = PhotoImage(file = "sunshine.gif")
image = canvas.create_image(50, 50, anchor=NE, image=filename)

line - Creates a line item.

line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)

oval - Creates a circle or an ellipse at the given coordinates. It takes two pairs of coordinates; the top left and bottom right corners of the bounding rectangle for the oval.

oval = canvas.create_oval(x0, y0, x1, y1, options)

polygon - It creates a polygon item that must have at least two vertices.

oval = canvas.create_polygon(x0, y0, x1, y1,...xn, yn, options)


Following is a simple example -

import Tkinter

top = Tkinter.Tk()

C = Tkinter.Canvas(top, bg="blue", height=250, width=300)

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



When the above code is executed, it will produce the following result -

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the Python Tkinter Checkbutton Widget.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain