Tkinter - How to put an outline on a canvas text

The create_text method of Canvas widget in Tkinter doesn't have an attribute like "outline" or "border" to set an outline around a text object. However, you can create an outline effect by using the text's bounding box to draw a rectangle around it.

Steps

  • Import the required libraries and create an instance of tkinter frame.

  • Set the size of the frame using root.geometry method.

  • Create a Canvas widget and set its height and width. Also, set its background color with background="white".

  • Next, create a text object inside the Canvas using create_text() method. Set the font and color of the text as shown in the example.

  • Get the bounding box (bbox) of the text item.

  • Use the bbox data to create a rectangle with an outline.

  • Finally, run the mainloop of the application window.

Example

# Import tkinter library
from tkinter import *

# Create an instance of tkinter frame or window
root = Tk()

# Set the geometry of tkinter frame
root.geometry("700x350")

# Create a Canvas
canvas = Canvas(root, background="white")
canvas.pack(expand=True)

# Create text inside the Canvas
text = canvas.create_text(175, 50, text="Text inside the Canvas", font="Calibri, 20", fill="green")

# Get the bounding box of text
bbox = canvas.bbox(text)

# Outline the canvas text
canvas.create_rectangle(bbox, outline="blue")

root.mainloop()

Alternative Approaches

Adding Padding to the Outline

You can add padding around the text by expanding the bounding box coordinates ?

from tkinter import *

root = Tk()
root.geometry("700x350")

canvas = Canvas(root, background="white")
canvas.pack(expand=True)

# Create text inside the Canvas
text = canvas.create_text(175, 50, text="Text with Padding", font="Calibri, 20", fill="green")

# Get the bounding box and add padding
bbox = canvas.bbox(text)
padding = 5
padded_bbox = (bbox[0] - padding, bbox[1] - padding, 
               bbox[2] + padding, bbox[3] + padding)

# Create rectangle with padding
canvas.create_rectangle(padded_bbox, outline="red", width=2)

root.mainloop()

Multiple Text Effects

You can create more complex outlines by combining multiple rectangles ?

from tkinter import *

root = Tk()
root.geometry("700x350")

canvas = Canvas(root, background="white")
canvas.pack(expand=True)

# Create text
text = canvas.create_text(350, 175, text="Styled Text", font="Arial, 24", fill="navy")

# Get bounding box
bbox = canvas.bbox(text)

# Create multiple outlines for shadow effect
canvas.create_rectangle(bbox[0]+2, bbox[1]+2, bbox[2]+2, bbox[3]+2, 
                       outline="gray", width=1)
canvas.create_rectangle(bbox, outline="black", width=2)

root.mainloop()

How It Works

The bbox() method returns a tuple containing four coordinates: (x1, y1, x2, y2) representing the top-left and bottom-right corners of the text's bounding rectangle. The create_rectangle() method then uses these coordinates to draw an outline around the text.

Conclusion

Use canvas.bbox() to get text boundaries and create_rectangle() to draw outlines. Add padding by expanding the bounding box coordinates for better visual appearance.

---
Updated on: 2026-03-26T18:32:28+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements