Tkinter: how to colorize the outline of a canvas rectangle?

user4772964 picture user4772964 · Apr 22, 2015 · Viewed 16.9k times · Source

I draw a rectangle on a Canvas:

 canvas = Canvas(parent,  cursor="cross")   
 rect = canvas.create_rectangle(20,20, 1, 1, fill="")

I only want to draw the border, leaving the interior transparent (that is why I set fill="" as mentioned here).

My problem:

I want the rectangle to have a red border. How can I do that?

Answer

martineau picture martineau · Apr 22, 2015

By default, the interior of a rectangle is empty, and you can also get this behavior with fill='' rather than just leaving it out.

If you want the rectangle outlined, just add a keyword argument named outline to the create_rectangle() call:

rect = canvas.create_rectangle(20,20, 1, 1, outline='red')

You can also control the width of the border by also adding a width=xxx keyword argument to the call. The default width is 1 pixel.