Tkinker Canvas

Tkinker Canvas is used to draw shapes like lines, polygons, ovals, and more in GUI.It’s like a drawing board on which you can paint or draw anything. It considered as one of the more advanced Tkinter widgets. Usin this we can also display images.

Syntax:

The Parameter master represents the parent window and options can be used as key-value pairs separated by commas.

Options of Tkinker Canvas:

bd: The size of the border around the indicator. Default is 2 pixels.

bg: The normal background color displayed behind the label and indicator.

highlightcolor: The color of the focus highlight.

confine: If true (the default), the canvas cannot be scrolled outside of the scroll region.

height: Size of the canvas in the Y dimension.

cursor: Cursor used in the canvas like arrow, circle, dot etc.

width: Size of the canvas in the X dimension.

relief: Relief specifies the type of the border. Some of the values are SUNKEN, RAISED, GROOVE, and RIDGE.

confine: If true (the default), the canvas cannot be scrolled outside of the scrollregion.

yscrollcommand: If the canvas is scrollable, this attribute should be the .set() method of the vertical scrollbar.

scrollregion: A tuple (w, n, e, s) that defines over how large an area the canvas can be scrolled, where w is the left side, n the top, e the right side, and s the bottom.

yscrollincrement: Works like xscrollincrement, but governs vertical movement.

xscrollincrement: If you set this option to some positive dimension, the canvas can be positioned only on multiples of that distance, and the value will be used for scrolling by scrolling units, such as when the user clicks on the arrows at the ends of a scrollbar.

xscrollcommand: If the canvas is scrollable, this attribute should be the .set() method of the horizontal scrollbar.

Standard items support by Canvas:

There are some standard items which are support by canvas , given below:

1- Image in Canvas:

It creates an image item, which can be an instance of either the PhotoImage classes or the BitmapImage.

#image in canvas

Imagename = Image(name = "flower.gif")
image = canvas.create_image(50, 50, anchor=NE, image=imagename)
2- Oval in Canvas:

It creates a circle or an ellipse at the given coordinates. It has two pairs of coordinates; One is the top left and the other is the bottom right corners of the bounding rectangle for the oval.

3- ARC in Canvas:

It creates an arc item, which can be a simple arc or a chord, a pieslice.

#arc in canvas

coord = 20, 40, 250, 200,
arc = canvas.create_arc(coord, start=0, extent=150, fill="Black")
4- Line in Canvas:

It creates a line item on canvas.

#line in canvas

line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)
5- Polygon in Canvas:

It creates a polygon item on canvas that must have at least three vertices.

#polygon in canvas

oval = canvas.create_polygon(x0, y0, x1, y1,...xn, yn, options)
Example:
from tkinter import *
from tkinter import messagebox


appWindow = Tk()
appWindow.title("Window Title-Tutorialsart.com")

appWindow.geometry('400x350')

frame = Frame(appWindow, width=300, height=300)
frame.pack(expand=True, fill=BOTH)

canvas = Canvas(frame, bg='black', width=300, height=300)

coordinates = 20, 50, 210, 230
arc = canvas.create_arc(coordinates, start=0, extent=250, fill="white")
arc = canvas.create_arc(coordinates, start=250, extent=50, fill="pink")
arc = canvas.create_arc(coordinates, start=300, extent=60, fill="brown")

canvas.pack(expand=True, fill=BOTH)

appWindow.mainloop()
Output:
18 tkinter canvas

Comments are closed.