How do I remove the light grey border around my Canvas widget?

The short answer is, the Canvas has two components which affect the edges: the border (borderwidth attribute) and highlight ring (highlightthickness attribute).

If you have a border width of zero and a highlight thickness of zero, the canvas coordinates will begin at 0,0. Otherwise, these two components of the canvas infringe upon the coordinate space.

What I most often do is set these attributes to zero. Then, if I actually want a border I'll put that canvas inside a frame and give the frame a border.


Section 6.8 Why doesn't the canvas seem to start at 0,0? of the Tk Usage FAQ describes the phenomenon.

I was able to eliminate the border artefact with slight changes to the posted source...

Change this:

w = Canvas(master, width=150, height=40, bd=0, relief='ridge')
w.pack()

to:

w = Canvas(master, width=150, height=40, bd=0, highlightthickness=0, relief='ridge')
w.pack()

and this:

x0 = 2
y0 = 2
x1 = 151
y1 = 2

to:

x0 = 0
y0 = 0
x1 = 150
y1 = 0

Interestingly enough, the "borderwidth" attribute did not make a difference, but I left it in per the FAQ.

Running w.config() immediately after the Canvas initialization statement showed the defaults to be 2 for highlightthickness and 0 for border width.