Canvas - stroke pen width

I am drawing a border around a canvas and want to set its width.

If I do this (where c is self.canvas_1) :

c.stroke_rect(0,0,c.get_width(),c.get_height())

it is a set width.

How do I set its width to, say, 5px?

My workaround is this :

self.border_width=5
c.fill_rect(0,0,c.get_width(),c.get_height())
c.clear_rect(self.border_width,self.border_width,c.get_width()-(self.border_width*2),c.get_height(), self.border_width*2))

which fills the canvas with colour then blanks out the middle.

The short answer to your question is that you should set c.line_width to the pixel width you want. That said, you should also read this explanation of Canvas pixel coordinates if you want things to look just right.

The following works nicely for me, as long as t, the border thickness you want, is an odd number:

    c = self.canvas_1
    
    w = c.get_width()
    h = c.get_height()
    t = 5
    c.line_width = t
    c.stroke_rect(t/2.0,t/2.0,w-t,h-t)

Of course, you could also just set the border property of the Canvas to β€œ5px solid blue” :wink:.

1 Like

Ok, I’m going mad. It’s official.

It had no border property when i looked yesterday.
i tried setting the line_width and it made no difference.

Today, the border property is there in the IDE and the line_width does indeed work.

I think I might have been awake too long yesterday …

(oh, and thanks).