Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Tkinter Canvas Widget


The Canvas widget in Tkinter is a versatile widget that can be used to draw shapes, such as lines, rectangles, ovals, and more. You can also use it to place images and text. Here's a step-by-step guide to using the Canvas widget in Tkinter.

1. Creating a Basic Canvas

First, let's create a basic canvas.

Example: Creating a Basic Canvas


python
                        import tkinter as tk

                        # Create the main window
                        root = tk.Tk()
                        root.title("Basic Canvas Example")
                        root.geometry("400x300")

                        # Create a Canvas widget
                        canvas = tk.Canvas(root, width=300, height=200, bg="white")
                        canvas.pack(pady=20)

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • tk.Canvas(root, width=300, height=200, bg="white"): Creates a Canvas widget with specified width, height, and background color.
  • canvas.pack(pady=20): Adds the Canvas widget to the window.


2. Drawing Shapes

You can draw various shapes on the canvas, such as lines, rectangles, ovals, polygons, and arcs.

Example: Drawing Shapes


python
                        import tkinter as tk

                        # Create the main window
                        root = tk.Tk()
                        root.title("Canvas Shapes Example")
                        root.geometry("400x300")
                                            
                        # Create a Canvas widget
                        canvas = tk.Canvas(root, width=300, height=200, bg="white")
                        canvas.pack(pady=20)
                                            
                        # Draw a line
                        canvas.create_line(10, 10, 200, 10, fill="black", width=2)
                                            
                        # Draw a rectangle
                        canvas.create_rectangle(50, 50, 150, 100, outline="blue", width=2, fill="lightblue")
                                            
                        # Draw an oval
                        canvas.create_oval(200, 50, 300, 150, outline="green", width=2, fill="lightgreen")
                                            
                        # Draw a polygon
                        canvas.create_polygon(150, 150, 200, 180, 250, 150, 200, 120, outline="purple", fill="lavender", width=2)
                                            
                        # Draw an arc
                        canvas.create_arc(10, 100, 100, 200, start=0, extent=180, outline="red", fill="pink", width=2)
                                            
                        # Run the application
                        root.mainloop()
                                            
                    

Explanation:

  • canvas.create_line(x1, y1, x2, y2, options): Draws a line from (x1, y1) to (x2, y2).
  • canvas.create_rectangle(x1, y1, x2, y2, options): Draws a rectangle with the top-left corner at (x1, y1) and bottom-right corner at (x2, y2).
  • canvas.create_oval(x1, y1, x2, y2, options): Draws an oval inside the bounding box defined by (x1, y1) and (x2, y2).
  • canvas.create_polygon(x1, y1, x2, y2, ..., options): Draws a polygon with the specified vertices.
  • canvas.create_arc(x1, y1, x2, y2, options): Draws an arc within the bounding box defined by (x1, y1) and (x2, y2).


3. Adding Text

You can add text to the canvas using the create_text method.

Example: Adding Text


python
                        import tkinter as tk

                        # Create the main window
                        root = tk.Tk()
                        root.title("Canvas Text Example")
                        root.geometry("400x300")

                        # Create a Canvas widget
                        canvas = tk.Canvas(root, width=300, height=200, bg="white")
                        canvas.pack(pady=20)

                        # Add text to the canvas
                        canvas.create_text(150, 100, text="Hello, Canvas!", font=("Arial", 20), fill="black")

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • canvas.create_text(x, y, options): Places text at the specified (x, y) coordinates.


4. Adding Images

You can add images to the canvas using the create_image method. Note that you need to use the PhotoImage class to load images.

Example: Adding Images


python
                        import tkinter as tk

                        # Create the main window
                        root = tk.Tk()
                        root.title("Canvas Image Example")
                        root.geometry("400x300")

                        # Create a Canvas widget
                        canvas = tk.Canvas(root, width=300, height=200, bg="white")
                        canvas.pack(pady=20)

                        # Load an image
                        image = tk.PhotoImage(file="path/to/your/image.png")

                        # Add the image to the canvas
                        canvas.create_image(150, 100, image=image)

                        # Keep a reference to the image to prevent it from being garbage collected
                        canvas.image = image

                        # Run the application
                        root.mainloop()

                    

Explanation:

  • tk.PhotoImage(file="path/to/your/image.png"): Loads an image from the specified file.
  • canvas.create_image(x, y, image=image): Places the image at the specified (x, y) coordinates.
  • canvas.image = image: Keeps a reference to the image to prevent it from being garbage collected.


5. Interactivity with Tags and Bindings

You can add interactivity to canvas items using tags and event bindings.

Example: Making Shapes Interactive


python
                        import tkinter as tk

                        def on_click(event):
                            canvas.itemconfig(event.widget.find_withtag(tk.CURRENT), fill="red")
                                            
                        # Create the main window
                        root = tk.Tk()
                        root.title("Canvas Interaction Example")
                        root.geometry("400x300")
                                            
                        # Create a Canvas widget
                        canvas = tk.Canvas(root, width=300, height=200, bg="white")
                        canvas.pack(pady=20)
                                            
                        # Draw shapes with tags
                        rect = canvas.create_rectangle(50, 50, 150, 100, outline="blue", width=2, fill="lightblue", tags="rect")
                        oval = canvas.create_oval(200, 50, 300, 150, outline="green", width=2, fill="lightgreen", tags="oval")
                                            
                        # Bind the click event to the shapes
                        canvas.tag_bind("rect", "", on_click)
                        canvas.tag_bind("oval", "", on_click)
                        
                        # Run the application
                        root.mainloop()
                        
                    

Explanation:

  • tags: Assigns tags to canvas items, allowing you to reference them easily.
  • canvas.tag_bind(tag, event, callback): Binds an event to a canvas item with the specified tag.
  • canvas.itemconfig(item, options): Configures the specified item with new options.

Summary

The Canvas widget in Tkinter is a powerful tool for creating custom drawings, shapes, images, and interactive graphics. By understanding how to use its methods and properties, you can create complex and dynamic visualizations in your Tkinter applications.







Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java