pythontkintertkinter-canvas

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


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?


Solution

  • 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 in a color other than the default 'black,' 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.