[Tutor] Border width of Canvas widget in tkinter

Peter Otten __peter__ at web.de
Wed Sep 19 09:15:17 CEST 2012


Alok Joshi wrote:

> I am using Python 3.x
> 
> I am unable to remove the border in a Canvas widget with bd=0 or
> borderwidth=0. Can someone please explain how one can do this?
> 
> I give below my program
> 
> class Avatar(Frame):
> def
> 
__init__(self,parent=None,width=100,height=100,ovalness=1,bgFrameColor='Blue',bgCanvasColor='Black'):
> Frame.__init__(self,parent,width=width,height=height,bg=bgFrameColor)
> self.grid() #self.grid_propagate(0)
> 
> self.width=width
> self.height=height
> self.ovalness=ovalness
> self.bgFrameColor=bgFrameColor
> self.bgCanvasColor=bgCanvasColor
> 
> 
self.canvas1=Canvas(self,width=width/2,height=height/2,bg=bgCanvasColor,borderwidth=0)
> self.canvas1.grid(row=0,column=0,ipadx=0,ipady=0,padx=0,pady=0)
> self.canvas1.grid_propagate(0)
> 
self.canvas2=Canvas(self,width=width/2,height=height/2,bg=bgCanvasColor,borderwidth=0)
> self.canvas2.grid(row=1,column=1,ipadx=0,ipady=0,padx=0,pady=0)
> self.canvas2.grid_propagate(0)
> 
> self.draw()
> def draw(self):
> pass
> 
> if __name__=='__main__':
> root=Tk()
> x=Avatar(parent=root)
> x.mainloop()
> 
> when I run this program I can see a gray border on the two canvas objects.

After some experimentation I could identify "highlightthickness" as the 
culprit:

import Tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bg="yellow")
frame.pack(fill="both", expand=True)
for i in range(2):
    canvas = tk.Canvas(frame, width=100, height=100, highlightthickness=0, 
bg="black")
    canvas.grid(row=i, column=i)
root.mainloop()

To get there I usually throw something like 

print list(canvas).config())

and look for promising candidates.



More information about the Tutor mailing list