2011-10-14 17 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'], 
    ); 
}