[英]tkinter default button in a widget
这似乎很简单...
我编写了一个对话框小部件,其中放置了一些条目,按钮等-其中一个我想通过鼠标单击来激活的按钮,也可以通过按回车键来激活。 不久前我读到,只需设置其默认选项,但我认为它在最新版本中已更改。
您知道如何设置吗?
谢谢!
将对'<Return>'
事件的回调绑定到窗口(在Tkinter中通常称为root
)或包含的帧。 让回调函数接受一个事件参数(可以忽略),并使其invoke()
按钮的回调函数。
root.bind('<Return>', (lambda e, b=b: b.invoke())) # b is your button
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()
myaction
和'QUIT',而Esc键都调用myquit
。 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.