【问题标题】:Perl Tk Key Binding to EnterPerl Tk 键绑定进入
【发布时间】:2020-06-16 10:29:29
【问题描述】:

我尝试制作一个具有 $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> ");
}

【问题讨论】:

    标签: perl tk key-bindings perltk


    【解决方案1】:

    这条线有几个问题:

    $cmd_entry->bind(ref $cmd_entry,'<Enter>',sub {sendLog("enter");});
    

    a) bind 不将条目小部件的引用作为第一个参数。

    b) '' 绑定标签是指通过以下方式输入小部件时的事件 鼠标或键盘,而不是回车键,即

    试试:

    $cmd_entry->bind('<Return>',sub {sendLog("enter");});
    $cmd_entry->bind('<Tab>',sub {sendLog("tab");});
    

    【讨论】:

    • 另外一个问题,在哪里可以找到 $widget->bind(tag,sequence,callback) 的序列列表?
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多