[英]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.