tkinter.simpledialog.Dialog leaves the initiating tkinter.Button depressed tkinter.simpledialog.Dialog leaves the initiating tkinter.Button depressed tkinter tkinter

tkinter.simpledialog.Dialog leaves the initiating tkinter.Button depressed


It's happening because you are partially overriding the default bindings which do the Right Thing.

If you want a button to execute a function on a button activation the proper way to do this is to add a command option to the button. The reason I use "activation" instead of "press" is — as your code shows — there is more than one way to activate a button in tk: button-presses, return-presses, accelerator key presses, etc.

The code you wrote does not replace the rather large set of default bindings. The answer by iCodez does correctly fix the most obvious defects, but letting the default bindings stand and using command= will work for the cases that you haven't tested (e.g. keyboard-only operation).


I have a simple, workaround solution for the third question. Actually, you yourself had the answer in your post. Change this line:

self.button.bind("<Button-1>", open_dialog_op)

to this:

self.button.bind("<ButtonRelease-1>", open_dialog_op)

Now, open_dialog_op is bound to the release of the mouse click. Meaning, the simpledialog will only open once the button comes back up (which is what its behavior should be).

However, I'd also like to make a suggestion. Why not use tkinter.messagebox.askokcancel for this? It is the same as the Windows' system prompts. See below:

tkinter.simpledialog.Dialog dialog:

enter image description here

tkinter.messagebox.askokcancel dialog:

enter image description here