Open In App

Create different shapes using Canvas class in Tkinter – Python

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Tkinter, the standard Python library for creating graphical user interfaces (GUIs), provides a powerful widget called the Canvas that allows us to draw and manipulate shapes. The Canvas widget in Tkinter is an excellent tool for building 2D graphics. Our task is to create various shapes such as ovals, rectangles, arcs, and polygons using the Canvas class.

Canvas Methods for Shapes

Here are some essential methods provided by the Canvas class to create various shapes:

  1. Canvas.create_oval(x1, y1, x2, y2, options = …): Used to create ovals, pie slices, and chords. The parameters (x1, y1) and (x2, y2) define the bounding box of the oval.
  2. Canvas.create_rectangle(x1, y1, x2, y2, options = …): Used to create rectangles and squares. The coordinates (x1, y1) and (x2, y2) define the opposite corners of the rectangle.
  3. Canvas.create_arc(x1, y1, x2, y2, options = …): Used to create arcs. Similar to the rectangle and oval, the coordinates (x1, y1) and (x2, y2) define the bounding box of the arc.
  4. Canvas.create_polygon(coordinates, options = …): Used to create any valid shape defined by a series of points. The coordinates are passed as a list of (x, y) pairs.

Drawing Shapes on the Canvas

This Python code uses Tkinter to create a simple GUI that displays various shapes on a canvas. The Shape class defines methods to draw ovals, rectangles, arcs, and polygons using the Canvas widget. These shapes are customized with different colors and line widths. The canvas is packed into the main window, and the program runs in the Tkinter event loop, allowing the shapes to be displayed interactively.

Python
from tkinter import *
from tkinter.ttk import *

class Shape:
    def __init__(self, master=None):
        self.master = master
        
        self.create()
    
    def create(self):
        
        self.canvas = Canvas(self.master)

        self.canvas.create_oval(10, 10, 80, 80, 
                            outline="black", fill="white",
                            width=2)
        
        self.canvas.create_oval(110, 10, 210, 80,
                            outline="red", fill="green",
                            width=2)
        
        self.canvas.create_rectangle(230, 10, 290, 60,
                                outline="black", fill="blue",
                                width=2)
        
        self.canvas.create_arc(30, 200, 90, 100, start=0,
                        extent=210, outline="green",
                        fill="red", width=2)
        
        points = [150, 100, 200, 120, 240, 180,
                210, 200, 150, 150, 100, 200]
        
        self.canvas.create_polygon(points, outline="blue",
                            fill="orange", width=2)

        self.canvas.pack(fill=BOTH, expand=1)

if __name__ == "__main__":
    
    master = Tk()
    shape = Shape(master)

    master.title("Shapes")
    
    master.geometry("330x220+300+300")

    mainloop()

Output:  

shapesUsingCanvasClass

Explanation:

  • The Shape class initializes the canvas and draws the shapes using methods like create_oval, create_rectangle, create_arc, and create_polygon.
  • The window size and position are set using geometry(), and the layout is managed with the pack() method.
  • The mainloop() keeps the application running and interactive.


Next Article
Practice Tags :

Similar Reads