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. So, to put an outline on a canvas text, you can follow the steps given below −

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.


# Import tkinter library
from tkinter import *

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

# Set the geometry of tkinter frame

# Create a Canvas
canvas = Canvas(root, background="white")

# 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")



On execution, it will produce the following output −

Updated on: 26-Oct-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started