如何使用Perl :: tk创建提示

时间:2013-07-03 22:17:19

标签: perl user-interface tk

那里有很多tk东西,但似乎没有一个我想要它做的...下面的CreateOk将创建一个消息框(没有另一个主窗口)并按下确定后返回结果。我想创建一个行为类似于ok框的框,但会提示用户输入文本并在单击提交按钮时返回文本。我也在下面发布了我的尝试。

sub CreateOk
{
   my ($statement, $title) = @_;
   my $return;

   if (defined($statement))
   {
      my $main_window = MainWindow->new();
      $main_window->withdraw();

      $return = $main_window->messageBox(
         -title   => $title // 'Ok pop-up',
         -message => $statement,
         -type    => 'OK',
         -icon    => 'question',
      );
   }
   else
   {
      cluck "No message given for OK pop-up!";
   }

   return($return);
}

这是我的尝试

sub CreatePrompt
{
   my ($question, $title) = @_;
   my $pop_up;

   if (defined($question))
   {
      my $main_window = MainWindow->new();

      my $winMain = MainWindow->new(
         -title => $title // 'User Input',
      );
      $winMain->Label(
         -text => $question
      )->pack( -side => 'left' );

      my $entName = $winMain->Entry(
         -textvariable => \$pop_up
      );
      $entName->pack( -side => 'left' );
      $winMain->Button(
         -text    => 'Show',
         -command => sub{ close; }
      )->pack( -side => 'left' );

      MainLoop();
   }
}

但这会创建主窗口和弹出窗口,我如何像以前一样创建弹出窗口?

1 个答案:

答案 0 :(得分:0)