Writing to the same text widget from two modules 
Author Message
 Writing to the same text widget from two modules

Greetings,

I need to write to a text widget from two different modules, the main
program and a module that main calls. I am able to write to it from the
main, but not from the called module. This is perhaps due more to my
lack of understanding of how Perl modules work than something specific
to Tk, but I decided to post here because I think Tk users are more
likely to know the solution. :-)

The test code follows. The comment at the top of the main program
explains the problem in more detail. Thank you very much in advance for
you help!

Jorge

---------------------------------------- main program
#!/usr/local/bin/perl5.005_02

#vx
#This simple test program creates a button and a text widget.
#When the button is pressed, text is written to the text widget.

#The purpose of this test is to learn how to write to the same
#text widget from two different sources, the main program (this)
#and a module the main program uses (vxm).

#Currently, I am able to write from the main program, but not
#from the module. That is, print_text() in the module is unable
#to write "Message from module.\n" to the text widget. What
#must I do to make it possible to write from the module as well?

#To execute, create directories bin/ and lib/ under the same
#parent directory, say, test/. Place in test/bin/ this program.
#Place in test/lib the module vxm.

use strict;
use Tk;

use FindBin qw($Bin);
use lib "$Bin/../lib";

use vxm;           #test module
use vars qw/$t/;

package main;

#Create the main window;
my $mw = MainWindow->new();
$mw->title ("Simple Sample");

#Button widget
my $button = $mw->Button(-text          => 'Main',
                         -command       => \&exec_request);
$button->pack();

#Text widget
$t = $mw->Scrolled('Text');
$t->pack(-expand        =>      1,
         -fill          =>      'both');
tie(*TEXT, 'Tk::Text', $t);

my $quit = $mw->Button(-text => 'Quit',
                       -height => 1,
                       -command => sub {exit});
$quit->pack();

MainLoop();

sub exec_request {
  if ($button->cget('text') eq "Main") {
    $button->configure(text => 'Module');
    print TEXT "Message from main.\n";
  } else {
    $button->configure(text => 'Main');
    my $res = print_text();
    if (!$res) {
      print TEXT "  Module returned error.\n";
    } else {
      print TEXT "  Module returned $res.\n";
    }
    print TEXT "Back to main.\n";
  }

Quote:
}

------------------------------------module vxm.pm

package vxm;
use strict;

use Exporter;


#vxm.pm
#This sample module is called by vx and
#needs to print to the text widget defined
#in the main program vx.

sub print_text {
    print TEXT "Message from module.\n";
    return 1;

Quote:
}

1;


Sun, 16 Jun 2002 03:00:00 GMT  
 Writing to the same text widget from two modules
After sending this message I realized that I could have resolved the
problem by passing a  reference to the text widget to the called function
print_text(). So in main, the line
my $res = print_text(); changes to my $res = print_text(\$t);.  And in the
module, the function print_text now becomes:

sub print_text {
    my $t_ref = shift;

    tie(*TEXT, 'Tk::Text', $$t_ref);
    print TEXT "Message from module.\n";
    return 1;

Quote:
}

Jorge
Quote:

> Greetings,

> I need to write to a text widget from two different modules, the main
> program and a module that main calls. I am able to write to it from the
> main, but not from the called module. This is perhaps due more to my
> lack of understanding of how Perl modules work than something specific
> to Tk, but I decided to post here because I think Tk users are more
> likely to know the solution. :-)

> The test code follows. The comment at the top of the main program
> explains the problem in more detail. Thank you very much in advance for
> you help!

> Jorge

> ---------------------------------------- main program
> #!/usr/local/bin/perl5.005_02

> #vx
> #This simple test program creates a button and a text widget.
> #When the button is pressed, text is written to the text widget.

> #The purpose of this test is to learn how to write to the same
> #text widget from two different sources, the main program (this)
> #and a module the main program uses (vxm).

> #Currently, I am able to write from the main program, but not
> #from the module. That is, print_text() in the module is unable
> #to write "Message from module.\n" to the text widget. What
> #must I do to make it possible to write from the module as well?

> #To execute, create directories bin/ and lib/ under the same
> #parent directory, say, test/. Place in test/bin/ this program.
> #Place in test/lib the module vxm.

> use strict;
> use Tk;

> use FindBin qw($Bin);
> use lib "$Bin/../lib";

> use vxm;           #test module
> use vars qw/$t/;

> package main;

> #Create the main window;
> my $mw = MainWindow->new();
> $mw->title ("Simple Sample");

> #Button widget
> my $button = $mw->Button(-text          => 'Main',
>                          -command       => \&exec_request);
> $button->pack();

> #Text widget
> $t = $mw->Scrolled('Text');
> $t->pack(-expand        =>      1,
>          -fill          =>      'both');
> tie(*TEXT, 'Tk::Text', $t);

> my $quit = $mw->Button(-text => 'Quit',
>                        -height => 1,
>                        -command => sub {exit});
> $quit->pack();

> MainLoop();

> sub exec_request {
>   if ($button->cget('text') eq "Main") {
>     $button->configure(text => 'Module');
>     print TEXT "Message from main.\n";
>   } else {
>     $button->configure(text => 'Main');
>     my $res = print_text();
>     if (!$res) {
>       print TEXT "  Module returned error.\n";
>     } else {
>       print TEXT "  Module returned $res.\n";
>     }
>     print TEXT "Back to main.\n";
>   }
> }

> ------------------------------------module vxm.pm

> package vxm;
> use strict;

> use Exporter;
> $VERSION = 1.00;


> #vxm.pm
> #This sample module is called by vx and
> #needs to print to the text widget defined
> #in the main program vx.

> sub print_text {
>     print TEXT "Message from module.\n";
>     return 1;
> }

> 1;



Sun, 16 Jun 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Writing content of a text widget

2. Tied variable to write in a text widget.

3. writing widget "redefining" modules

4. Comparison of two fields in two text files

5. widget demo for new text module features.

6. Q: Changing Text-Color in Text-Widget?

7. text formatting in a Text widget

8. How update text in text widget when executing some other callback

9. Color Coding Text in a Tk::Text Widget

10. Checking for marked or selected text in a Text widget

11. Grabbing Text from Text widget

12. Retrieving a line of text from a Text Widget when clicking on it

 

 
Powered by phpBB® Forum Software