2011-10-14 18 views
3

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

如果您运行它,屏幕将每隔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'], 
    ); 
} 

回答

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'], 
    ); 
}