Default tkinter button in widgets

it seemed easy ...

I wrote a dialog widget in which I put some entries, buttons, etc. - among which is the button that I would activate by clicking the mouse, but also by pressing the return button. I read a while ago that you just need to set your default option, but I think it has changed in recent versions.

Do you know how to install it?

thank!

+5
source share
2 answers

Bind the callback to the event '<Return>'on a window (often called rootin Tkinter) or on the containing frame. Ask callback to accept the event parameter (which you can ignore) and have invoke()your button callback.

root.bind('<Return>', (lambda e, b=b: b.invoke())) # b is your button
+6
source
def myaction():
    print('Your action in action')

def myquit():
    root.destroy()

root = Tk()
label = Label(root, text='Label Text').pack(expand=YES, fill=BOTH)
label.bind('<Return>', myaction)
label.bind('<Key-Escape>', myquit)
ok = Button(label, text='My Action', command=myaction).pack(side=LEFT)
quit_but = Button(label, text='QUIT', command=myquit).pack(side=RIGHT)
root.mainloop()
  1. .
  2. , " " "Return" myaction "QUIT", Esc myquit.

, .

0

All Articles