Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Create different shapes using Canvas class in Tkinter

The Canvas widget in tkinter allows you to draw multiple shapes, such as lines, rectangles, circles (ovals), arcs, and polygons. You can also add text and images.

In this tutorial, we will discuss how to create various shapes using the Canvas class in tkinter.

Creating Different Shapes Using Canvas in tkinter:

1. Import Required Libraries:

import tkinter as tk

2. Initialize Main Application Window:

root = tk.Tk()
root.title("Canvas Shapes Tutorial")

3. Create and Pack the Canvas Widget:

canvas = tk.Canvas(root, bg="white", width=400, height=400)
canvas.pack(pady=20)

4. Drawing Different Shapes:

i. Drawing a Line:

To draw a line, you use the create_line method.

canvas.create_line(10, 10, 200, 50, fill="blue")

ii. Drawing a Rectangle:

For a rectangle, you use the create_rectangle method.

canvas.create_rectangle(50, 50, 150, 150, fill="green")

iii. Drawing an Oval (or Circle):

For ovals (and circles), use the create_oval method.

canvas.create_oval(50, 200, 150, 250, fill="yellow") # For oval
canvas.create_oval(200, 50, 250, 100, fill="red")   # For circle

iv. Drawing a Polygon:

Polygons can be created using the create_polygon method.

canvas.create_polygon(200, 200, 240, 240, 220, 270, fill="pink")

v. Drawing an Arc:

To draw an arc, use the create_arc method.

canvas.create_arc(250, 250, 350, 350, start=0, extent=180, fill="orange")

vi. Placing Text:

Text can be added with the create_text method.

canvas.create_text(300, 300, text="Tkinter Canvas", font=("Arial", 14))

5. Mainloop to Run the Application:

root.mainloop()

Complete Code:

import tkinter as tk

root = tk.Tk()
root.title("Canvas Shapes Tutorial")

canvas = tk.Canvas(root, bg="white", width=400, height=400)
canvas.pack(pady=20)

# Drawing shapes
canvas.create_line(10, 10, 200, 50, fill="blue")
canvas.create_rectangle(50, 50, 150, 150, fill="green")
canvas.create_oval(50, 200, 150, 250, fill="yellow")
canvas.create_oval(200, 50, 250, 100, fill="red")
canvas.create_polygon(200, 200, 240, 240, 220, 270, fill="pink")
canvas.create_arc(250, 250, 350, 350, start=0, extent=180, fill="orange")
canvas.create_text(300, 300, text="Tkinter Canvas", font=("Arial", 14))

root.mainloop()

When you run the provided code, you will get a window showcasing all the different shapes drawn on the canvas. You can adjust the coordinates, colors, and other parameters to customize the appearance of your shapes.

  1. Python Tkinter draw shapes on Canvas example:

    • Description: Tkinter's Canvas widget allows you to draw various shapes like lines, rectangles, circles, and polygons.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      canvas = tk.Canvas(root, width=300, height=200)
      canvas.pack()
      
      # Draw a line
      canvas.create_line(50, 50, 200, 50, fill="blue")
      
      # Draw a rectangle
      canvas.create_rectangle(50, 100, 150, 150, fill="red")
      
      # Draw an oval
      canvas.create_oval(200, 100, 250, 150, fill="green")
      
      root.mainloop()
      
  2. Creating circles, rectangles, and polygons in Tkinter:

    • Description: You can create circles, rectangles, and polygons on the Tkinter Canvas using the create_oval, create_rectangle, and create_polygon methods.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      canvas = tk.Canvas(root, width=300, height=200)
      canvas.pack()
      
      # Draw a circle
      canvas.create_oval(50, 50, 150, 150, fill="blue")
      
      # Draw a rectangle
      canvas.create_rectangle(170, 50, 270, 150, fill="red")
      
      # Draw a polygon
      canvas.create_polygon(100, 170, 150, 200, 200, 170, fill="green")
      
      root.mainloop()
      
  3. Drawing lines and ellipses on Tkinter Canvas:

    • Description: You can draw lines and ellipses on the Tkinter Canvas using the create_line and create_oval methods.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      canvas = tk.Canvas(root, width=300, height=200)
      canvas.pack()
      
      # Draw a line
      canvas.create_line(50, 50, 200, 50, fill="blue")
      
      # Draw an ellipse
      canvas.create_oval(50, 100, 200, 150, fill="red")
      
      root.mainloop()
      
  4. Python Tkinter Canvas create_rectangle example:

    • Description: The create_rectangle method in Tkinter's Canvas widget is used to draw rectangles.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      canvas = tk.Canvas(root, width=300, height=200)
      canvas.pack()
      
      # Draw a filled rectangle
      canvas.create_rectangle(50, 50, 150, 150, fill="blue")
      
      # Draw an outlined rectangle
      canvas.create_rectangle(170, 50, 270, 150, outline="red")
      
      root.mainloop()
      
  5. Customizing shapes on Canvas in Tkinter:

    • Description: Shapes on the Tkinter Canvas can be customized using various options like fill, outline, width, etc.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      canvas = tk.Canvas(root, width=300, height=200)
      canvas.pack()
      
      # Customize a rectangle
      canvas.create_rectangle(50, 50, 150, 150, fill="blue", outline="red", width=2)
      
      root.mainloop()
      
  6. Creating geometric patterns with Tkinter Canvas:

    • Description: Tkinter's Canvas widget allows you to create geometric patterns by combining various shapes.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      canvas = tk.Canvas(root, width=300, height=200)
      canvas.pack()
      
      # Create a checkerboard pattern
      for i in range(0, 300, 50):
          for j in range(0, 200, 50):
              canvas.create_rectangle(i, j, i+25, j+25, fill="black")
      
      root.mainloop()
      
  7. Drawing arcs and ovals in Tkinter with Canvas:

    • Description: You can draw arcs and ovals on the Tkinter Canvas using the create_arc and create_oval methods.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      canvas = tk.Canvas(root, width=300, height=200)
      canvas.pack()
      
      # Draw an arc
      canvas.create_arc(50, 50, 150, 150, start=0, extent=180, fill="blue")
      
      # Draw an oval
      canvas.create_oval(170, 50, 270, 150, fill="red")
      
      root.mainloop()
      
  8. Python Tkinter Canvas polygon drawing:

    • Description: You can draw polygons on the Tkinter Canvas using the create_polygon method.
    • Code:
      import tkinter as tk
      
      root = tk.Tk()
      
      canvas = tk.Canvas(root, width=300, height=200)
      canvas.pack()
      
      # Draw a filled polygon
      canvas.create_polygon(50, 50, 150, 50, 100, 150, fill="green")
      
      root.mainloop()