What is canvas in tkinter?
In tkinter, the canvas widget is used to draw graphics.
You can draw:
- Text
- Widgets
- Graph and plots
- Frames
A tkinter canvas is a rectangular area.
See the examples below for drawing various elements in the canvas widget.
A simple example of creating a canvas
In the first example, we just created a canvas widget without any graphics to show how it looks.
Code:
from tkinter import *
win = Tk()
#Creating window
win.geometry("500x500")
win.title("Create a Canvas")
#Creating canvas
can = Canvas(win,bg = "#004000",height = "250")
can.pack()
win.mainloop()
Output:

Items that can be created in canvas
Tkinter canvas supports following items:
- Line
- Arc
- Oval
- Text
- Image
- Rectangle
See the examples below of creating these items with their respective methods.
Creating a line in canvas example
For creating a line in the canvas, you may use the create_line() method. See an example below where we created a line in the canvas in yellow color:
Code:
from tkinter import *
win = Tk()
#Creating window
win.geometry("500x500")
win.title("Create Canvas with a line")
#Creating canvas
can = Canvas(win,bg = "#004000",height = "250")
#Creating yellow color line in canvas
line = can.create_line((100, 100), (300, 200), width=4, fill='#FFFF00')
can.pack()
win.mainloop()
Output:

Creating an arc in canvas example
Use create_arc() method for creating an arc in the canvas.
In the program below, we created an arc by providing arc coordinates.
Code:
from tkinter import *
win = Tk()
#Creating window
win.geometry("500x500")
win.title("Create Canvas with an arc")
#Creating canvas
can = Canvas(win,bg = "#804000",height = "300")
#Creating an arc in the canvas
arc = can.create_arc((15,20,240,210),start = 50,extent = 90, fill= "#FFD7D7")
can.pack()
win.mainloop()
Output:

Creating an oval shape example
The syntax for creating an oval is:
create_oval(*args, **kw)
Where arg is two coordinate points.
See an example below where we created a shape like a chicken egg:
Code:
from tkinter import *
win = Tk()
#Creating window
win.geometry("500x500")
win.title("Create Canvas with an oval shape")
#Creating canvas
can = Canvas(win,bg = "#804000",height = "300")
#specify oval points
co_pnt = (
(70, 50),
(220, 250),
)
#Creating an oval in the canvas
ova = can.create_oval(co_pnt, fill='#FFFFFF')
can.pack()
win.mainloop()
Output:

An example of polygon in canvas
In the example below, we created a polygon by specifying its points.
Code:
from tkinter import *
win = Tk()
#Creating window
win.geometry("500x500")
win.title("Create Canvas with a polygon")
#Creating canvas
can = Canvas(win,bg = "#804000",height = "300")
#specify polygon points
poly_pnt = (
(50, 150),
(100, 100),
(150, 150),
)
#Creating a polygon
poly_g = can.create_polygon(poly_pnt, fill='#FFFF5E')
can.pack()
win.mainloop()
Output:

Creating a text entry in canvas example
The following program creates an input entry box inside the canvas.
Python program:
from tkinter import *
win = Tk()
#Creating window
win.geometry("500x500")
win.title("Create Canvas with an entry textbox")
#Creating canvas
can = Canvas(win,height = "300")
textentry = Entry(can)
can.create_window(600, 50, window=textentry, height=50, width=1000)
can.pack()
win.mainloop()
Output:

For detailed options and features available with canvas in tkinter, visit this documentation:
https://tkinter-docs.readthedocs.io/en/latest/widgets/canvas.html