How To Use Canvas In Tkinter In Python

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

  • bd- The Border is used to change the width in pixels. Default pixels value is 2.
  • bg - The background is used to change Normal background color background.
  • fg - The fg is used to change the normal foreground (text) color.
  • font - The font is used to change the text font to be used for the button's label.
  • height - The height is used to change the height of the button in text lines (text buttons) or pixels (images).
  • relief-Relief is used to specify the type of the border. The values are SUNKEN, RAISED, GROOVE, and RIDGE.
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
  • Scroll region-The scroll region is used to tuple (w, n, e, s) that defines over how large the canvas can be scrolled; for example west =left side, north = top, east = right side, and south = bottom.
  • xscrollincrement-The xscrollincrement is moved in a horizontal direction.
  • yscrollincrement-The yscrollincrement is moved in a vertical direction.
  • xscrollcommand- The xscrollcommand keyword used for the canvas is scrollable, this attribute must be the. set () method of the horizontal scrollbar.
  • yscrollcommand- The yscrollcommand keyword used for the canvas is scrollable, this attribute must be the. set () method of the vertical scrollbar.
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.   
  7. list1.configure(yscrollcommand=scroll.set)  
  8. list1.pack(side=LEFT)  
  9. scroll.pack(side=RIGHT,fill=Y)  
  10. for q in range(500):  
  11.     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!


Similar Articles