The Canvas widget provides graphic facilities in a rectangular area where various shapes and layouts can be created with the Canvas
class methods in Python.
The
Canvas
class is defined in the Tkinter Python package.
Any shape that a Canvas
class creates requires a canvas
object to be packed to the main window.
Let’s have a look at few of the methods defined in the Canvas
class:
Canvas.create_oval(x1,y1,x2,y2)
: This method takes in two pairs of coordinates as arguments and returns an oval or similar shape.
Canvas.create_rectangle(x1,y1,x2,y2)
: This method takes in two pairs of coordinates as arguments and returns a rectangle or a square.
Canvas.create_polygon(co_ords)
: This method takes in coordinates as arguments and returns any valid shape.
Canvas.create_arc(x1,y1,x2,y2)
: This method takes in two pairs of coordinates as arguments and returns an arc.
Have a look at the code snippet below that illustrates how to use the Python Canvas widget to draw an arc:
from tkinter import *t = Tk()canvas = Canvas(t, bg="green", height=250, width=250)coordinates = 30, 70, 260, 230arc = canvas.create_arc(coordinates, start=0, extent=120, fill="blue")canvas.pack()t.mainloop()
Executing the code snippet above will pop up a window that displays a blue arc of 120 degrees on a green background.
Free Resources