In this tutorial, I will explain how to master the Python Tkinter canvas widget. As a developer based in the USA, I have used the Tkinter Canvas to build various projects and faced real-world challenges along the way. I will share my insights and provide detailed examples to help you master the Tkinter Canvas.
Master the Python Tkinter Canvas
The Tkinter Canvas is a versatile widget that allows you to display and manipulate graphical elements in your Python applications. It provides a wide range of features for drawing shapes, lines, text, images, and more. To get started with the Tkinter Canvas, you need to import the necessary modules and create a Canvas object within your Tkinter window.
from tkinter import *
window = Tk()
canvas = Canvas(window, width=400, height=400)
canvas.pack()In this example, we import the Tkinter module, create a window using Tk(), and then create a Canvas object with a specified width and height. The pack() method is used to display the Canvas within the window.
Read Python Tkinter Autocomplete
Example 1. Draw Shapes and Lines on the Canvas
One of the primary uses of the Tkinter Canvas is to draw various shapes and lines. Let’s say you want to create a simple drawing application where users can draw rectangles and lines. Here’s an example of how you can achieve this:
def draw_rectangle():
canvas.create_rectangle(50, 50, 200, 100, fill="blue")
def draw_line():
canvas.create_line(0, 0, 400, 400, width=2, fill="red")
rectangle_button = Button(window, text="Draw Rectangle", command=draw_rectangle)
rectangle_button.pack()
line_button = Button(window, text="Draw Line", command=draw_line)
line_button.pack()You can see the output in the screenshot below.

In this code snippet, we define two functions: draw_rectangle() and draw_line(). These functions use the create_rectangle() and create_line() methods of the Canvas object to draw a blue rectangle and a red line, respectively. We also create two buttons that trigger these functions when clicked.
Check out Registration form in Python using Tkinter
Example 2. Handle User Interactions with Canvas Elements
The Tkinter Canvas allows you to handle user interactions with the graphical elements. You can bind events to specific elements or the entire Canvas. Let’s consider an example where you want to create an interactive map of the United States. When the user hovers over a state, its name should be displayed.
state_coords = {
"California": [50, 100, 200, 150],
"Texas": [250, 200, 400, 300],
# Add coordinates for other states
}
def display_state_name(event):
x, y = event.x, event.y
for state, coords in state_coords.items():
if coords[0] <= x <= coords[2] and coords[1] <= y <= coords[3]:
state_label.config(text=state)
break
canvas.bind("<Motion>", display_state_name)
state_label = Label(window, text="")
state_label.pack()You can see the output in the screenshot below.

In this example, we define a dictionary state_coords that stores the coordinates of each state on the Canvas. The display_state_name() function is triggered whenever the mouse moves over the Canvas. It checks the coordinates of the mouse pointer against the coordinates of each state. If the pointer is within the boundaries of a state, its name is displayed in the state_label.
Read BMI Calculator Using Python Tkinter
Example 3. Create Interactive Games with Tkinter Canvas
The Tkinter Canvas is not limited to static graphics; it can also be used to create interactive games. One popular example is the classic Snake game. Let’s see how you can build a basic version of Snake using the Tkinter Canvas.
import random
# Game constants
CANVAS_WIDTH = 800
CANVAS_HEIGHT = 800
SNAKE_SIZE = 30
FOOD_SIZE = 25
SPEED = 200
# Initialize the snake and food
snake = [(200, 200), (220, 200), (240, 200)]
food = (random.randint(0, CANVAS_WIDTH - FOOD_SIZE), random.randint(0, CANVAS_HEIGHT - FOOD_SIZE))
def move_snake():
global snake, food
head = snake[-1]
new_head = (head[0] + dx, head[1] + dy)
if new_head in snake or new_head[0] < 0 or new_head[0] >= CANVAS_WIDTH or new_head[1] < 0 or new_head[1] >= CANVAS_HEIGHT:
game_over()
return
snake.append(new_head)
if new_head == food:
food = (random.randint(0, CANVAS_WIDTH - FOOD_SIZE), random.randint(0, CANVAS_HEIGHT - FOOD_SIZE))
else:
snake.pop(0)
canvas.delete("all")
draw_snake()
draw_food()
window.after(SPEED, move_snake)
def draw_snake():
for segment in snake:
canvas.create_rectangle(segment[0], segment[1], segment[0] + SNAKE_SIZE, segment[1] + SNAKE_SIZE, fill="green")
def draw_food():
canvas.create_oval(food[0], food[1], food[0] + FOOD_SIZE, food[1] + FOOD_SIZE, fill="red")
def change_direction(event):
global dx, dy
if event.keysym == "Left" and dx != SNAKE_SIZE:
dx = -SNAKE_SIZE
dy = 0
elif event.keysym == "Right" and dx != -SNAKE_SIZE:
dx = SNAKE_SIZE
dy = 0
elif event.keysym == "Up" and dy != SNAKE_SIZE:
dx = 0
dy = -SNAKE_SIZE
elif event.keysym == "Down" and dy != -SNAKE_SIZE:
dx = 0
dy = SNAKE_SIZE
def game_over():
canvas.delete("all")
canvas.create_text(CANVAS_WIDTH // 2, CANVAS_HEIGHT // 2, text="Game Over!", fill="red", font=("Arial", 24))
dx = SNAKE_SIZE
dy = 0
window.bind("<KeyPress>", change_direction)
move_snake()
window.mainloop()You can see the output in the screenshot below.

In this Snake game implementation, we define the game constants such as canvas size, snake size, food size, and game speed. The snake is represented as a list of coordinates, and the food is randomly placed on the Canvas.
The move_snake() function is responsible for moving the snake, checking for collisions, and updating the game state. The draw_snake() and draw_food() functions handle the rendering of the snake and food on the Canvas.
User input is handled by binding the arrow keys to the change_direction() function, which updates the direction of the snake’s movement.
Check out How to Create Countdown Timer using Python Tkinter
Conclusion
In this tutorial, I have explained how to master the Python Tkinter canvas widget. I discussed how to draw shapes and lines on canvas, handle user interaction with canvas elements, and create interactive games with Tkinter canvas.
You may like to read:

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.