Search code examples
pythonwidgettkinter

tkinter default button in a widget


this seemed easy...

I wrote a dialog widget where I put some entries, buttons etc - among which a button I'd like to activate by mouse click, but also by pressing return. I read time ago that it was just necessary to set its default option, but I think that it changed in recent versions.

Do you know how it is possible to set it ?

thanks!


Solution

  • Bind a callback to the '<Return>' event to the window (often called root in Tkinter) or to the containing frame. Have the callback accept an event parameter (which you can ignore) and have it invoke() your button's callback.

    root.bind('<Return>', (lambda e, b=b: b.invoke())) # b is your button