2017-02-23 78 views

回答

3

默認行爲被實現爲內部tk類的綁定。對於按鈕,該類是"Button"

要添加新的行爲,您可以在類名上使用bind_class,假定您想要所有tkinter按鈕的此行爲。同樣,要刪除默認行爲,可以將unbind_class與類名一起使用。您必須在創建根窗口後執行此操作。

import Tkinter as tk # python 2.7 
# import tkinter as tk # python 3.x 

root = tk.Tk() 

# invoke the button on the return key 
root.bind_class("Button", "<Key-Return>", lambda event: event.widget.invoke()) 

# remove the default behavior of invoking the button with the space key 
root.unbind_class("Button", "<Key-space>") 
+0

是的,這將做到這一點 – user3364161