Computer >> Computer tutorials >  >> Programming >> Python

How to colorize the outline of a canvas rectangle in Tkinter?


Let us suppose we have created a rectangle on a Tkinter canvas. The task is to provide the rectangle with an outline that can have a color in it. To provide a border or outline to a rectangle, first define the outline property in the constructor and add a new color value to it.

Example

In this example, we will create a rectangle on a Tkinter canvas and then apply a color to its outline.

#Import the required libraries
from tkinter import *

#Create an instance of Tkinter Frame
win = Tk()

#Set the geometry
win.geometry("700x350")

# Define a Canvas Widget
canvas = Canvas(win, width=500, height=350)
canvas.pack()

# Create a rectangle in Canvas
canvas.create_rectangle(100,100,300,300, outline= 'yellow', width=4, fill='green')

win.mainloop()

Output

Running the above code will display a window with a rectangle inside the canvas widget.

How to colorize the outline of a canvas rectangle in Tkinter?