Tkinter relief buttons when clicked python

13,412

Solution 1

You can tie a mouse-click to a function that enforces the relief of the button to keep it flat, otherwise the mouse-click will use its default action on click: making the button sunken.

def keep_flat(event):       # on click,
    if event.widget is btn: # if the click came from the button
        event.widget.config(relief=FLAT) # enforce an option

def callback():
    print('button clicked')

root = Tk()

btn = Button(root, text='click', relief=FLAT, command=callback)
btn.pack()

root.bind('<Button-1>', keep_flat) # bind the application to left mouse click

mainloop()

Another option is to bind the button-click to the string "break", which prevents any typical event handlers from kicking off (including the button's command option):

btn.bind('<Button-1>', lambda e: 'break')

http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

Solution 2

Setting -borderwidth (-bd) to 0 worked for me. The button text keeps moving down showing the button is being pressed, but sunken border doesn't appear.

Share:
13,412
Kevin
Author by

Kevin

hello

Updated on June 05, 2022

Comments

  • Kevin
    Kevin almost 2 years

    So, in Tkinter Buttons seem to have a relief property. This is defaulted to raised, and when the button is pressed it changes to sunken. I made my use relief=FLAT, but when I click this button it still is sunken. Is there a way to make it so that there is a different relief when clicked? Would it involve changing the border color?

    Button(text=day, width=2,
           relief=FLAT, activebackground = self.color_clicked,
           background = self.today_color)
    

    This looks flat, but when I click on it it changes to sunken (obviously it changes back to flat when I let go, however).