Canvas in Tkinter
Tkinter canvas .
The canvas widget is used to add graphics in the python application. We can draw different graphics and shapes.
syntax : c = Canvas( master,**options)
here master means the parent window
and options means its attributes .
In this Part of canvas you will learn how to create canvas and add some basic graphical shapes using canvas methods .
Create a Canvas in Tkinter Window .
# Tkinter Tutorial : Dynamic Coding
# canvas Widget .....
# Creating Canvas .....
# Import library
from tkinter import *
root = Tk()
root.title("Dynamic Coding")
canvas = Canvas(root, width=300,
height=300,bg='cyan')
canvas.pack(fill = BOTH)
root.mainloop()
Output .
Add line on canvas .
synatx : line = canvas.create_line(x0, y0, x1, y1, ..., xn, yn, options)
create_line method is used to create line on canvas .
# Tkinter Tutorial : Dynamic Coding
# canvas Widget .....
# Creating Canvas .....
# Import library
from tkinter import *
root = Tk()
root.title("Dynamic Coding")
canvas = Canvas(root, width=300,
height=300,bg='cyan')
canvas.pack(fill = BOTH)
# adding line on canvas ..
#canvas.create_line(0,150,300,150,width=5,fill='black')
root.mainloop()
Output .
Create a rectangle :
square = canvas.create_rectangle(0,0,150,150, fill="green")
create_rectangle takes four positional arguments representing the top-left and bottom-right coordinates of the rectange, and then a list of optional named parameters. In this case we set the fill colour to green.
# Tkinter Tutorial : Dynamic Coding
# canvas Widget .....
# Creating Canvas .....
# Import library
from tkinter import *
root = Tk()
root.title("Dynamic Coding")
canvas = Canvas(root, width=300, height=300,bg='cyan')
canvas.pack(fill = BOTH)
# creating a rectangle ...
# you can use both way to code it .
#rect = canvas.create_rectangle(100,150,200,200,
#fill='green',width=5,outline='red')
canvas.create_rectangle(100,150,200,200,
fill='green',width=5,outline='red')
root.mainloop()
Output .
Other Methods available are :
arc − Creates an arc item, which can be a chord, a pieslice or a simple arc.
coord = 10, 50, 240, 210
arc = canvas.create_arc(coord, start=0, extent=150, fill="blue")
image − Creates an image item, which can be an instance of either the BitmapImage or the PhotoImage classes.
filename = PhotoImage(file = "image.extension")
image = canvas.create_image(50, 50, anchor=NE, image=filename)
oval − Creates a circle or an ellipse at the given coordinates. It takes two pairs of coordinates; the top left and bottom right corners of the bounding rectangle for the oval.
oval = canvas.create_oval(x0, y0, x1, y1, options)
Text -- Add a text on the caanvas
text = canvas.create_text(230,230, text="Text on canvas", fill="purple",
font=("Helvectica", "16"))
Deleting items from the canvas
You may have noticed that we've been storing the value returned by the create methods used to add shapes to the canvas. Each creation method returns an object indentifier, which allows us to manipulate the objects after they have been added.
You can delete any item from the canvas by using canvas.delete:
canvas.delete(square)
canvas.delete('all')
Attributes / options of canvas ...
S no. | Option | Discription |
---|---|---|
1 | bd | bd displays the border width |
2 | bg | bg displays the background color of the canvas |
3 | confine | This property doesn’t let you scroll outside the scroll region. |
4 | cursor | You can use the cursor as different shapes on the screen as circle, dot etc. |
5 | height | Displays the size of the canvas as in the Y dimension. |
6 | highlightcolor | Displays the highlighted color in focus |
7 | relief | This displays different types of borders like SUNKEN, RAISED, GROOVE, and RIDGE. |
8 | scrollregion | Allows how much the area can be scrolledin TOP, RIGHT, LEFT and BOTTOM poistions. |
9 | width | Sets the width of the canvas |
No comments