pythontkintercanvastkinter-canvas

Change the highlight background thickness of a Canvas?


I know how to change the color of the highlightbackground of a Canvas using tkinter, but I am not sure how to change the thickness of it. When I try to change the baz['highlightbackgroundthickness'] or the baz['highlightbackgroundwidth'], it errors out. Please help, thanks!

Sample code:

from tkinter import Tk, Frame, Canvas

foo = Tk()
bar = Frame(foo, bg = "white")
bar.grid()
baz = Canvas(bar, width=50, height=50, bg = 'blanched almond')
baz.grid(row=0, column=0)
baz['highlightbackground'] = 'blue' #I know how to change this
baz['highlightbackgroundthickness'] = 2 #Error
baz['highlightbackgroundwidth'] = 2 #Error
#enter code for changing highlight background thickness here

Solution

  • Thanks to @acw1668 and @nikost's comments, it turns out the option to change that is highlightthickness. See below code!

    from tkinter import Tk, Frame, Canvas
    
    foo = Tk()
    bar = Frame(foo, bg = "white")
    bar.grid()
    baz = Canvas(bar, width=50, height=50, bg = 'blanched almond')
    baz.grid(row=0, column=0)
    baz['highlightbackground'] = 'blue'
    baz['highlightthickness'] = 2 #change the highlight thickness!
    

    Thanks to @OysterShucker, I learned that you can print out the keys of a Canvas to find the one you want doing this:

    from tkinter import Canvas
    
    print(Canvas.keys())