Lyzeum Lyzeum - 1 year ago 67
Python Question

Tkinter Button doesn´t change it´s relief after pressing it

Why does my tkinter Button stays in the "sunken" relief after I press it?

import tkinter
from tkinter import messagebox as msgbox

class GUI(object):
def __init__(self):
self.root = tkinter.Tk()

self.testButton = tkinter.Button(self.root, text="Click Me!")
self.testButton.bind("<ButtonRelease-1>", self.release)

def release(self, event):

def click(self, event):
result = msgbox.askokcancel("Continue?", "Do you want to continue?")
if result:
print("Well then . . .")

if __name__ == "__main__":
test = GUI()

The console shows that the relief is "raised" but on the GUI it stays in the "sunken" relief , why?
The GUI after pressing the Button

Answer Source

Your callback is printing "raised" because your code is run before the default button bindings, so the button relief is in fact raised at the point in time when your function is called.

I'm pretty sure this is what is causing the button to stay sunken:

  1. you click on the button, and a dialog appears. At this point the button is raised because tkinter's default binding has not yet had a chance to run 1, and it is the default bindings which cause the button to appear sunken
  2. a dialog appears, which steals the focus from the main window.
  3. you click and release the button to click on the dialog. Because the dialog has stolen the focus, this second release event is not passed to the button
  4. at this point the processing of the original click continues, with control going to the default tkinter binding for a button click.
  5. the default behavior causes the button to become sunken
  6. at this point, your mouse button is not pressed down, so naturally you can't release the button. Because you can't release the button, the window never sees a release event.
  7. Because the button never sees a button release event, the button stays sunken

1 For a description of how tkinter handles events, see this answer: The answer is focused on keyboard events, but the same mechanism applies to mouse buttons.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download