1

Enterキーボードのボタンをバインドしようとしています。私はPerl Tkで書いています。

私は次のことのいずれかが起こることを望んでいます。

  1. キーボードで「Enter」を押すと、プログラム内のボタンが動作するようにします。
  2. キーボードで「Enter」を押すと、サブルーチンが実行されます。私のボタンがサブルーチンを開くことを見て、どちらも私のプログラムを満足させます。

これが私の関連コードです:

# Button
my $enterbut = $find_sub->Button(
    -command => \&find_substations,
    -text => 'Find Displays',
    -background => 'gray'
)->pack(
    -side => 'left',
    -fill => 'none',
    -ipadx => 8,
    -ipady => 1
);

# Accept "Enter" key as input
$enterbut->bind('<Return>', \&find_substations);

# Output Substation ID to Pane
sub find_substations {
    print;
}

bind コマンドを使用するいくつかの異なる方法を試しましたが、どれも機能しません。

エラーは発生していませんが、ボタンを押してもサブが動作しません。Returnキーボードの正しいボタンではないかもしれないと思い始めました。ドライバーの言語か何かが原因かもしれません。たぶんコーディングエラー。

4

1 に答える 1

2

You are binding Enter on your button. It is captured only when your button is focused. If you bind it on the whole window, pressing Enter anywhere in the window should execute the handler function.

于 2013-12-24T15:48:34.767 に答える