Introduction

In this article, I will explain how to use canvas in Tkinter in Python.
Definition
The Canvas keyword is used to draw and do layout in a Python application. The Canvas is a square area intended for pictures or complex layouts. You can place graphics and text on the Canvas keyword. When you call the canvas function the first letter must be a capital letter.
Syntax
  1. c=Canvas (a, option=cost) //first letter must be capital letter.
Parameters
a − This represents the parent window of the computer.
option − The list of most used options for this widget.

Option & Description

Example
line − line keyword is used to create a line item.
  1. line =create_line(x0, y0, x1, y1)
Simple Program
  1. from tkinter import *
  2. a=Tk()
  3. c = Canvas(root, scrollregion=(0,0,500,500), height=200, width=200)
  4. c.pack()
  5. c.create_line(0,0,600,500,width=5,fill="red",dash=(3,3))
  6. c.create_line(0,500,600,0,width=5,fill="green",dash=(3,3))
  7. a.mainloop()
Output
How To Use Canvas In The Tkinter In Python
Example
arc −arc keyword is used to create an arc item, you want to specify some value in the arc function.
  1. arc = create_arc(coord, start=0, extent=180, fill="red")
oval −oval keyword is used to create a circle at the given coordinates. It will take two coordinates, the top left and bottom right corners.
  1. oval = canvas.create_oval(x0, y0, x1, y1, options)
Program
  1. from tkinter import *
  2. a=Tk()
  3. c=Canvas(a,width=600,height=500)
  4. c.pack()
  5. c.create_oval(100,100,300,300,width=7,fill="red")
  6. c.create_arc(100,100,300,300,start=0,extent=100,width=7,fill="yellow")
  7. a.mainloop()
Output
How To Use Canvas In The Tkinter In Python
Program
  1. from tkinter import *
  2. a=Tk()
  3. a.title("list and scrollbar")
  4. list1=Listbox(a,width=25,height=25,justify=CENTER,bg="pink",bd=20)
  5. scroll=Scrollbar(a,command=list1.yview)
  6. list1.configure(yscrollcommand=scroll.set)
  7. list1.pack(side=LEFT)
  8. scroll.pack(side=RIGHT,fill=Y)
  9. for q in range(500):
  10. list1.insert(END,q)
Output
How To Use Canvas In The Tkinter In Python

Conclusion

In this article, we have seen how to use canvas in Tkinter in Python. I hope this article was useful to you. Thanks for reading!