Signup/Sign In

Python Tkinter Canvas Widget

Tkinter Canvas widget is mainly used as a general-purpose widget which is used to draw anything on the application window in Tkinter.

  • This widget is mainly used to draw graphics and plots, drawings, charts, and showing images.

  • You can draw several complex layouts with the help of canvas, for example, polygon, rectangle, oval, text, arc bitmap, graphics, etc.

  • Canvas widget is also used to create graphical editors.

  • There are a number of options available to configure and control the Canvas Widget.

Tkinter Canvas Widget

The syntax of the canvas widget is given below:

w = Canvas(master, option=value)

In the above syntax, the master parameter denotes the parent window. You can use many options to change the layout of the canvas and these options are written as comma-separated key-values.

Tkinter Canvas Widget Options:

Following are the various options used with canvas widgets:

Option name Description
bd

This option is mainly used to set the width of the border in pixels.

The default value of 0px means no border, 1px means thin line border and you can increase the width of the border.

bg This option is used to set the background color.
cursor Whether to use an arrow, dot, or circle on the canvas for the cursor, this option can be used.
confine This option is set to make the canvas non-scrollable outside the scroll region.
height This option is used for controlling the height of the canvas.
width This option is used to set the width of the widget.
highlightcolor This option indicates the highlight color when there is a focus on the button
xscrollcommand In the case, if the canvas is of scrollable type, then this attribute should act as the set() method of the horizontal scrollbar
yscrollcommand In the case, if the canvas is of scrollable type, then this attribute should act as the set() method of the vertical scrollbar
scrollregion This is option is mainly used to represent the coordinates that are specified as the tuple containing the area of the canvas
xscrollincrement If the value of this option is set to a positive value then, the canvas is placed only to the multiple of this value.
yscrollincrement It is mainly used for vertical movement and it works in the same way xscrollincrement option works.

Tkinter Canvas Widget Basic Example

Let us create a simple canvas with the help of the canvas widget:

from tkinter import *   
  
# window named top
top = Tk()  
# set height and width of window 
top.geometry("300x300")  
  
#creating a simple canvas with canvas widget  
cv = Canvas(top, bg = "yellow", height = "300")  
  
cv.pack()  
  
top.mainloop()


The above code will create a simple canvas with background color yellow and you can draw anything above it.

Tkinter Canvas Widget - Pie Chart using Arcs

Let us create a canvas and then an arc on it with the help of code snippet given below:

import tkinter

# init tk
root = tkinter.Tk()

# creating canvas
mCanvas = tkinter.Canvas(root, bg="white", height=300, width=300)

# drawing two arcs
coord = 10, 10, 300, 300
arc1 = mCanvas.create_arc(coord, start=0, extent=150, fill="pink")
arc2 = mCanvas.create_arc(coord, start=150, extent=215, fill="blue")

# adding canvas to window and display it
mCanvas.pack()
root.mainloop()


The above code will open a window, then add a canvas, and then draws two arcs on it. The two arcs drawn of pink and blue color together make up a circle as shown in the output above.

Summary:

In this tutorial, we learned about the Tkinter canvas widget which can be used to draw anything on the canvas, maybe a chart, and image, or some dynamic shape.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight