Curses :: UI :: Dialog不响应输入密钥

时间:2011-10-14 15:08:36

标签: perl ncurses curses

我正在尝试创建一个Curses :: UI应用程序。到目前为止一切正常,但我的对话框似乎不想响应关闭的回车键。我已经尝试了样本并且它们可以工作但是出于某种原因,如果我这样做,Dialog不会响应按键。看,主窗口中的内容将最终填满屏幕并每隔x秒刷新一次,因此我希望对话框覆盖屏幕并在输入时关闭。这是我的测试脚本中的代码。

如果你运行它,屏幕将每10秒更新一次,显示左侧的时间。更新后,点击X以显示虚拟对话框。在下次更新时,屏幕数据将覆盖仍处于活动状态的对话框。按Enter键退出对话框,然后退出。

我的目标是将此对话框放在所有内容之上。

#!/usr/local/bin/perl

use strict;
use warnings;
use Curses::UI;

my ($dialog, $main, $ui, $container, $content);
my $last_update = 0;
my $first_run = 0;
$ui = Curses::UI->new(
    -color_support  =>  1,
    -mouse_support  =>  0,
    -border         =>  1,
    -debug          =>  0
);

$main = $ui->add(
    "main", "Window",
    -bfg        =>  "black",
    -x      =>  0,
    -y      =>  0,
    -height     =>  $ui->height,
    -width      =>  $ui->width
);

$main->focus();

$ui->set_binding( sub { $ui->leave_curses; exit(0); }, "q");
$ui->set_binding( \&exit, "x");
$ui->add_callback("callback", \&callback );
$ui->{-read_timeout} = 0.1;
$ui->mainloop;

sub callback {
    if($first_run == 0) {
        update_body();
        $first_run = 1;
    }

    my $now = time;
    if($now - $last_update >= 10) {
        update_body();
        $last_update = time;
    }
}

sub update_body {
    for(my $x = 0; $x < 2000; $x++) {
        $main->delete("body$x");
    }

    for(my $x = 0; $x < ($ui->height - 5); $x++) {
        my $now = time;
        $main->add(
            "body$x",       "Label",
            -x      =>  0,
            -y      =>  $x,
            -text       =>  $now,
            -width      =>  $ui->width
        )->draw();
    }
}

sub exit {
    my $return = $ui->dialog(
        -message   => "Test dialog",
        -title     => "Test",
        -buttons   => ['ok'],
    );
}

1 个答案:

答案 0 :(得分:0)

您必须不删除并重绘条目字段,有一个修改版本:

#!/usr/bin/perl

use strict;
use warnings;
use Curses::UI;

my ($dialog, $main, $ui, $container, $content);
my $last_update = 0;
my $first_run = 0;
$ui = Curses::UI->new(
    -color_support  =>  1,
    -mouse_support  =>  0,
    -border         =>  1,
    -debug          =>  0
);

$main = $ui->add(
    "main", "Window",
    -bfg        =>  "black",
    -x      =>  0,
    -y      =>  0,
    -height     =>  $ui->height,
    -width      =>  $ui->width
);

$main->focus();

$ui->set_binding( sub { $ui->leave_curses; exit(0); }, "q");
$ui->set_binding( sub { exit(0) if wexit(); }, "x");
$ui->add_callback("callback", \&callback );
$ui->{-read_timeout} = 0.1;
$ui->mainloop;

sub callback {
    if($first_run == 0) {
        draw_body();
        $first_run = 1;
    }

    my $now = time;
    if($now - $last_update >= 2) {
        update_body();
        $last_update = time;
    }
}
my @fields;
sub draw_body {
    for(my $x = 0; $x < ($ui->height - 5); $x++) {
        my $now = time;
        push @fields, $main->add(
            "body$x",       "Label",
            -x      =>  0,
            -y      =>  $x,
            -text       =>  $now,
            -width      =>  $main->width
        );
    }
    $ui->draw();
}
sub update_body {
    map { $_->text(time())  } @fields;
    $ui->draw();
}
sub wexit {
    return $ui->dialog(
        -message   => "Test dialog",
        -title     => "Test",
        -buttons   => ['ok'],
    );
}