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

How to colorize the outline of a Tkinter canvas rectangle?


We can create different shapes such as rectangles, arcs, circles, etc., on a Tkinter canvas. Canvas widget has many inbuilt functions and methods which can be used to configure the property of the shapes.

To colorize the outline of a canvas rectangle, we have to specify the color value in the outline property. It applies to the function create_rectangle(top, left, bottom, right) where the outline should be visible.

Example

#Import the required libraries
from tkinter import *

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

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

#Create a canvas object
canvas= Canvas(win, width= 300, height= 350)

#Create a rectangle in canvas
canvas.create_rectangle(300,200,10,10, outline= "red", fill= "white")
canvas.pack()

win.mainloop()

Output

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

How to colorize the outline of a Tkinter canvas rectangle?

Observe that we have set the outline of the rectangle as Red.