我已将 RouMao 的解决方案编辑到下面的代码示例中。
根据stackoverflow 上对这个问题的公认答案,我应该能够通过终止附加“\n”的文本来向 Wx::TextCtrl 添加额外的行。
my $t = Wx::TextCtrl->new( $p, -1, "$title\n" ,[-1,-1],[600,260]);
$t->{TERM}->AppendText( "another line\n");
不幸的是,这似乎不起作用,如下面的代码所示。我在 Windows XP 上运行草莓 Perl。
package main;
use Modern::Perl;
WxMultiLineCtrl->new()->MainLoop();
package WxMultiLineCtrl;
use base qw(Wx::App);
use Wx qw (wxVERTICAL wxTOP wxGROW wxHORIZONTAL wxTE_MULTILINE
wxFIXED_MINSIZE wxLEFT );
use Wx::Event qw( EVT_BUTTON );
sub OnInit {
my $app = shift ;
my $title='MultiLine Wx Text Control';
my $frame = Wx::Frame->new( undef ,-1,$title,[-1, -1],[640,280]);
my $p = Wx::Panel->new( $frame, -1);
my $v0= Wx::BoxSizer->new(wxVERTICAL);
my $h1= Wx::BoxSizer->new(wxHORIZONTAL);
my $h2= Wx::BoxSizer->new(wxHORIZONTAL);
my $term = Wx::TextCtrl->new( $p, -1
, "$title\n"
, [-1,-1],[600,260]
, wxTE_MULTILINE
);
my $cancelBtn = Wx::Button->new( $p, -1, "cancel" ,[-1,-1],[-1,-1]);
my $addTxtBtn = Wx::Button->new( $p, -1, "add text",[-1,-1],[-1,-1]);
$p->{TERM}=$term;
EVT_BUTTON( $p, $cancelBtn, \&cancel );
EVT_BUTTON( $p, $addTxtBtn, \&addTxt );
$v0->Add($h1,1,wxLEFT);
$v0->Add($h2,1,wxLEFT);
$h1->Add( $term , 1, wxTOP | wxGROW , 5 );
$h2->Add( $cancelBtn , 1, wxTOP | wxFIXED_MINSIZE , 5 );
$h2->Add( $addTxtBtn , 1, wxTOP | wxFIXED_MINSIZE , 5 );
$p->SetSizer($v0);
$p->SetAutoLayout(1);
$app->SetTopWindow($frame);
$frame->Show(1);
}
sub addTxt {shift->{TERM}->AppendText( "another line\n");}
sub cancel {exit;}