我尝试制作一个具有$ cmd_entry的GUI来接受输入,并在按下“ enter”键后在$ log_frame上打印输入。但是,绑定不能很好地工作。我不知道为什么回调函数有时会起作用,但有时却不起作用。当我将键绑定更改为时,当我按两次“ Tab”键时,它将起作用一次。
use Tk;
use Tk::ROText;
my $configuration_window = MainWindow->new(-title => "Testing");
$configuration_window->geometry("1024x800");
my $log_frame = $configuration_window->Scrolled("ROText", -scrollbars => 'os',-background => "white",-foreground => "black")->pack(-side => 'left', -expand => 1, -fill => 'both', -padx => 4, -pady => 4);
my $list_frame = $configuration_window->Frame(-borderwidth => 1, -relief => 'groove')->pack(-side => 'right', -fill => 'both', -expand => 1, -padx => 4, -pady => 4);
my $cmd_entry = $log_frame->Entry(-background => "white")->pack(-side => "bottom", -fill => 'x');
$cmd_entry->bind(ref $cmd_entry,'<Enter>',sub {sendLog("enter");});
$log_frame->insert('end', "> ");
MainLoop;
sub sendLog{
my ($text) = @_;
$log_frame->insert('end', "$text\n> ");
}
此行有两个问题:
$cmd_entry->bind(ref $cmd_entry,'<Enter>',sub {sendLog("enter");});
a)绑定不将对条目小部件的引用作为第一个参数。
b)绑定标签是指通过进入窗口小部件时的事件 鼠标或键盘,而不是Enter键,即。
尝试:
$cmd_entry->bind('<Return>',sub {sendLog("enter");});
$cmd_entry->bind('<Tab>',sub {sendLog("tab");});