Introduction
Basic Widgets
Toplevel Widgets
Geometry Management
Binding Functions
Working with Images in Tkinter
Tkinter Advance
Applications and Projects
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
.
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:
To draw a line, you use the create_line
method.
canvas.create_line(10, 10, 200, 50, fill="blue")
For a rectangle, you use the create_rectangle
method.
canvas.create_rectangle(50, 50, 150, 150, fill="green")
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
Polygons can be created using the create_polygon
method.
canvas.create_polygon(200, 200, 240, 240, 220, 270, fill="pink")
To draw an arc, use the create_arc
method.
canvas.create_arc(250, 250, 350, 350, start=0, extent=180, fill="orange")
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.
Python Tkinter draw shapes on Canvas example:
Canvas
widget allows you to draw various shapes like lines, rectangles, circles, and polygons.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()
Creating circles, rectangles, and polygons in Tkinter:
create_oval
, create_rectangle
, and create_polygon
methods.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()
Drawing lines and ellipses on Tkinter Canvas:
create_line
and create_oval
methods.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()
Python Tkinter Canvas create_rectangle example:
create_rectangle
method in Tkinter's Canvas
widget is used to draw rectangles.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()
Customizing shapes on Canvas in Tkinter:
fill
, outline
, width
, etc.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()
Creating geometric patterns with Tkinter Canvas:
Canvas
widget allows you to create geometric patterns by combining various shapes.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()
Drawing arcs and ovals in Tkinter with Canvas:
create_arc
and create_oval
methods.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()
Python Tkinter Canvas polygon drawing:
create_polygon
method.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()