2016-05-15 115 views
0

我只想要一个多行文本条目。perl tk text:无法将文本内容转换为变量

所以我用TK :: Text代替了TK :: Entry。

use Tk; 

my $mw = MainWindow->new(-width => '1000', -relief => 'flat', 
    -height => '840', -title => 'Test', -background => 'white',);  
$mw->geometry("1000x840+200+200"); 

my $desc = $mw->Scrolled('Text', -scrollbars => 'e', 
    -width => 50, -height => 3)->place(-x => 10, -y => 170); 

my $goButton = $mw->Button(-pady => '1', -relief => 'raised', 
    -padx => '1', -state => 'normal', -justify => 'center', 
    -text => 'Go', -width => 15, -height => 1, 
    -command => sub {$mw->destroy;})->place(-x => 12, -y => 770); 

my $cancelButton = $mw->Button(-pady => '1', -relief => 'raised', 
    -padx => '1', -state => 'normal', -justify => 'center', 
    -text => 'Cancel', -width => 8, -height => 1, 
    -command => sub { exit 0; })->place(-x => 140, -y => 770); 

$mw -> MainLoop(); 

print $desc->get('1.0'); 

但是当我运行此代码,我得到这个错误:

未能AUTOLOAD 'Tk的框架:: ::获得'

我在做什么错?

谢谢!

回答

2

$ mw-> MainLoop()设置一个循环,等待鼠标,键盘,计时器以及您使用的任何其他事件。 $ desc->获取( '1.0');将不会执行,直到您退出应用程序。你可以将它移动到上面,这将解决你所问的问题。

但是,您真正的问题是要将文本导入例如Entry()并在您的应用程序中使用它。查看一个很好的教程,例如http://docstore.mik.ua/orelly/perl3/tk/ch05_02.htm

更新16 5月:你想要做什么:在窗口中输入文本,然后按Go?试试这个:

use strict; 
use warnings; 
use Tk; 

my $mw = MainWindow->new(-width => '1000', -relief => 'flat', 
    -height => '840', -title => 'Test', -background => 'white',); 
$mw->geometry("1000x840+200+200"); 

my $desc = $mw->Text(-width => 50, -height => 3)->place(-x => 10, -y => 170); 

my $goButton = $mw->Button(-pady => '1', -relief => 'raised', 
    -padx => '1', -state => 'normal', -justify => 'center', 
    -text => 'Go', -width => 15, -height => 1, 
    -command => sub {\&fromGo($desc) })->place(-x => 12, -y => 770); 
my $cancelButton = $mw->Button(-pady => '1', -relief => 'raised', 
    -padx => '1', -state => 'normal', -justify => 'center', 
    -text => 'Cancel', -width => 8, -height => 1, 
    -command => sub { exit 0; })->place(-x => 140, -y => 770); 
$mw->MainLoop(); 


sub fromGo 
{ 
    my($desc) = @_; 
    my $txt = $desc->get('1.0', 'end-1c'); 
    print "$txt\n"; 
} 
+0

如果我的主循环之前,移动打印,将打印空白行...我不明白... –

+0

也进入无法处理多行... –

+0

对于多线进入:是的,使用文本(),但请阅读文本教程,例如我链接到的书,并给它最好的尝试。这本书很棒。 – Jorgen