2017-02-23 2 views
3

tkinterでは、ハイライト表示されたボタンをスペースバーで押すことができます。どのようにリターンキーに変更するのですか?特定の機能をボタンにバインドしたくない場合は、ボタンが強調表示されている場合にボタンを押すキーを変更したいと思います。Tkinterスペースボタンの代わりにenterボタンを押す

答えて

3

デフォルトの動作は、内部tkクラスのバインディングとして実装されています。ボタンの場合、そのクラスは"Button"です。

新しい動作を追加するには、すべてのtkinterボタンでこの動作を行うことを前提として、クラス名にbind_classを使用できます。同様に、デフォルトの動作を削除するには、クラス名に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

関連する問題