3
在tkinter中,您可以按空格鍵突出顯示的按鈕。我如何將其更改爲返回鍵?我不想將特定功能綁定到按鈕上,如果按鈕突出顯示,我想更改按下按鈕的按鈕。Tkinter按下輸入鍵而不是空格鍵
在tkinter中,您可以按空格鍵突出顯示的按鈕。我如何將其更改爲返回鍵?我不想將特定功能綁定到按鈕上,如果按鈕突出顯示,我想更改按下按鈕的按鈕。Tkinter按下輸入鍵而不是空格鍵
默認行爲被實現爲內部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>")
是的,這將做到這一點 – user3364161