Python Tkinter Canvas Introduction

Python Tkinter Canvas

The canvas is a general purpose widget, which is typically used to display and edit graphs and other drawings.

This is a highly versatile widget which can be used to draw graphs and plots, create graphics editors, and implement various kinds of custom widgets.

By using Tkinter canvas, you can draw several widgets in the canvas: arc bitmap, images, lines, rectangles, text, pieslices, ovals, polygons, ovals, polygons, and rectangles etc.

Syntax:

  							   
 w = canvas(parent, options) 


Example 1: Simple Button

Output:


Example 2:

This will print Welcome to Aimtocode"after clicking on "Click me" button.

Output: Simple Circle


Example 3: Drawing using canvas


Output: Before drawing

Output: After drawing

You can set the following important properties to customize a Canvas:

Option Description
bd It represents the border width. The default width is 2.
bg It represents the background color of the canvas.
confine This is used to set to make the canvas unscrollable outside the scroll region.
cursor It is used as the arrow, circle, dot, etc. on the canvas.
height It represents the height of the canvas in vertical direction.
highlightcolor It represents the highlight color when the widget is focused.
relief It Used to represent the type of the border. It can have the values as SUNKEN, RAISED, GROOVE, and RIDGE.
scrollregion It represents the coordinates specified as the tuple containing the area of the canvas.
width It represents the width of the canvas.
xscrollincrement If it is set to a positive value. The canvas is placed only to the multiple of this value.
xscrollcommand If the canvas is scrollable, this attribute should be the .set() method of the horizontal scrollbar.
yscrollincrement Works like xscrollincrement, but governs vertical movement.
yscrollcommand If the canvas is scrollable, this attribute should be the .set() method of the vertical scrollbar.