- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to put an outline on a canvas text on Tkinter?
Tkinter Canvas widget can be used for many purposes such as adding images, creating and drawing shapes in the canvas, animating the shapes and objects, etc. Using the Canvas inbuilt functions and methods, we can create and display the text.
To create a text, we use create_text(x,y, text, **options) method. To add an outline around the text in Canvas, we have to create the bounding box around the text. The bounding box property links the invisible box with the widget. And, this will allow us to put a rectangle in the text.
Once we have created a rectangle, we can pull this behind and make the text above the rectangle. The rectangle must be given an outline property that surrounds the canvas item.
Example
# Import the required libraries from tkinter import * # Create an instance of tkinter frame or window win=Tk() # Set the size of the window win.geometry("700x350") # Create a canvas widget canvas=Canvas(win, bg="blue3") canvas.pack() # Create a text in canvas text=canvas.create_text(100,200, text="This works only in canvas", font=('Calibri 18'), anchor="w", fill="white") # Make the bounding-box around text bbox=canvas.bbox(text) # Create a rectangle inside the bounding box rect=canvas.create_rectangle(bbox, outline="yellow", fill="black", width=5) # Make the text above to the rectangle canvas.tag_raise(text,rect) win.mainloop()
Output
If we will run the above code, it will display a window with a predefined text in the canvas. The text would have an outline visible on the canvas.
- Related Articles
- Tkinter - How to put an outline on a canvas text
- How to change the thickness of a shape's outline on a Tkinter canvas?
- How to draw an arc on a tkinter canvas?
- How to colorize the outline of a Tkinter canvas rectangle?
- How to create a Button on a Tkinter Canvas?
- How to draw a line on a Tkinter canvas?
- How to colorize the outline of a canvas rectangle in Tkinter?
- How to draw a dashed line on a Tkinter canvas?
- How to add text inside a Tkinter Canvas?
- How to draw a png image on a Python tkinter canvas?
- How to put a Tkinter window on top of the others?
- Measure text height on an HTML5 canvas element
- How do I update images on a Tkinter Canvas?
- How to draw a dot on a canvas on a click event in Tkinter Python?
- How to open PIL Image in Tkinter on Canvas?
