0

All I wanted is a multiline text entry.

So I used TK::Text instead of 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');

But when I run this code, I get this error:

Failed to AUTOLOAD 'Tk::Frame::get'

What am I doing wrong?

Thanks!

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
AJ Gottes
  • 401
  • 1
  • 3
  • 15

1 Answers1

2

$mw->MainLoop() sets up a loop waiting for events from mouse, keyboard, timer and whatever else you use. $desc->get('1.0'); will not be executed until you exit the application. You can move it above and that will solve the problem you ask.

However, your real problem is to get the text into for example Entry() and use it in your application. Check out a good tutorial such as for example http://docstore.mik.ua/orelly/perl3/tk/ch05_02.htm.

UPDATE 16 May: Is what you want to do:enter text in window and then press Go? Try this:

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";
}
Jorgen
  • 195
  • 1
  • 10
  • if I move the print before the mainloop, it will print blank line... I don't understand... – AJ Gottes May 15 '16 at 14:52
  • For multi-line entry: yes, use Text(), but please read the tutorial for Text in for example the book I linked to and give it your best try. The book is excellent. – Jorgen May 15 '16 at 15:01
  • You should also try running the command called widget from the command line (at least on Linux). widget is an interactive tutorial. – Jorgen May 15 '16 at 15:07