Skip to Content
Course content

384: Working with tkinter Canvas for Custom Drawing

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Think of the tkinter.Canvas widget as a physical whiteboard in a conference room. When you stand in front of it, you aren't just placing pre-made stickers on the board; you're using markers to draw specific lines, circles, and boxes. To do this, you need a way to tell your hand exactly where to go. You might say, "Start two inches from the left and three inches down from the top," and then "draw a line to a point ten inches across."

That's exactly how the Canvas works. It gives you a coordinate system where (0, 0) is the top-left corner. As x increases, you move right. As y increases, you move down. It's a bit counter-intuitive if you're used to high school geometry where Y goes up, but in the world of GUI programming, we almost always count down from the top.

Mapping Out Your Space

When you initialize a Canvas, you're essentially defining the size of your whiteboard. But the real magic happens when you call methods like create_line or create_oval. I want you to notice a pattern here: most Canvas drawing methods require a bounding box defined by two points: (x1, y1) and (x2, y2).

import tkinter as tk

root = tk.Tk()
# We're creating a 400x400 drawing area
canvas = tk.Canvas(root, width=400, height=400, bg="white")
canvas.pack()

# A line from (50, 50) to (150, 150)
canvas.create_line(50, 50, 150, 150, fill="blue", width=3)

# An oval is defined by the rectangle that would enclose it
# (x1, y1) is top-left, (x2, y2) is bottom-right
canvas.create_oval(200, 50, 300, 150, fill="red")

root.mainloop()

I've found that the biggest hurdle for people is the create_oval method. You aren't providing a center point and a radius; you're providing the corners of an imaginary box that the circle fits perfectly inside. If your x1 and y1 are the same distance apart as your x2 and y2, you get a circle. If not, you get an ellipse.

Giving Your Drawings a Name

Here is where the Canvas becomes powerful: every object you draw is actually a unique item that the Canvas remembers. If you draw a circle, the Canvas gives it an ID (an integer), but remembering "Item 4" is a nightmare. Instead, we use tags.

Think of tags like sticky notes you slap onto a drawing. You can tag a circle as "server_node" and a line as "connection". Later, you can tell Python, "Find everything tagged 'server_node' and turn it green," without having to track individual ID numbers.

# Create a rectangle and give it a tag
canvas.create_rectangle(100, 200, 200, 250, fill="gray", tags="status_bar")

# Later, we can change the color of everything with that tag
canvas.itemconfig("status_bar", fill="green")

Shifting Elements on the Fly

Unlike a static image, the Canvas is dynamic. You don't have to erase the whole board and redraw everything to move an object. You can use the move method. I personally love using this for simple animations or interactive diagrams. You just provide the tag (or ID) and the amount of pixels to shift it by on the X and Y axes.

For example, canvas.move("status_bar", 10, 0) would slide your rectangle 10 pixels to the right. It doesn't matter where it started; it just offsets from its current position. This is significantly more efficient than recalculating coordinates every time something changes.




📋 Practical Task

Build a Mini-Network Topology Diagram

Your task is to create a visual representation of a small computer network using a tkinter.Canvas. Your program should meet the following requirements:

  • Create a canvas with a light-gray background.
  • Draw three circles (representing "Servers") at different positions. Give each of them the tag "node".
  • Draw lines (representing "Cables") connecting the servers so they form a triangle.
  • Add a "Reset" button using a standard tkinter.Button. When clicked, this button should use canvas.itemconfig to change the color of all objects tagged "node" to a random color (or a specific color like "yellow").
  • Ensure the servers are filled with a distinct color (e.g., "blue") initially.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.