Difference between revisions of "Studio:Can I use Perl's Tk package in STRIDE?"

From STRIDE Wiki
Jump to: navigation, search
Line 4: Line 4:
  
 
Script to launch the Tk script. (Note: The Tk script has to be included in the workspace script files).
 
Script to launch the Tk script. (Note: The Tk script has to be included in the workspace script files).
 
+
<source lang="perl">
 
  use strict;
 
  use strict;
 
  use File::Spec;
 
  use File::Spec;
Line 31: Line 31:
 
   $main::ascript->MessageBox("$data[$i]", "Element $i");
 
   $main::ascript->MessageBox("$data[$i]", "Element $i");
 
  }
 
  }
 +
</source>
 
            
 
            
 
And here is the Tk script  
 
And here is the Tk script  
 
+
<source lang="perl">
 
  #!c:/perl/bin/perl -w
 
  #!c:/perl/bin/perl -w
 
  use strict;
 
  use strict;
Line 70: Line 71:
 
    
 
    
 
  }     
 
  }     
         
+
</source>         
  
 
[[Category:Perl Info]]
 
[[Category:Perl Info]]

Revision as of 18:49, 10 March 2008

We recommend running the script which uses the Tk package as a separate script outside of STRIDE Studio, as there is an incompatibility with the Tk package that causes errors during execution. You should not get an error the first time you run the script, but you will get errors during every subsequent execution until you shut down Studio. This is due to a problem with initialization and the Perl engine failing to fully clean up; the Tk engine relies on the cleanup code, but since this only occurs when the engine unloads (i.e., Studio is closed), errors occur during execution.

To run the script using Tk outside of Studio, you can launch it from a script in Studio using the system command. You can also pass arguments between the scripts. Below is an example.

Script to launch the Tk script. (Note: The Tk script has to be included in the workspace script files).

 use strict;
 use File::Spec;
 
 #Get path and name of perl exectuable          
 my $perlExe = $^X;
 if ($perlExe =~ /\.dll$/i) 
 {
     my ($vol, $dir, $file) = File::Spec->splitpath($perlExe);
     $perlExe = File::Spec->catpath($vol, $dir, 'perl.exe'); 
 }
 
 # get the name and path of the script with the Tk library
 my $tkScript= $main::studio->Workspace->Files->Item("tk.pl");
 my $tkFullPath = File::Spec->catfile($tkScript->Path, $tkScript->Name); 
 
 #List of arguments for Tk script
 my @options = ('"option 1"', '"option 2"', '"option 3"', '"option 4"');
 
 #Start the script 
 my @data = `$perlExe \"$tkFullPath\" @options`; 
 
 #@data is the array with the return values (through STDOUT)
 for (my $i=0; $i <= $#data; $i++)
 {
   $main::ascript->MessageBox("$data[$i]", "Element $i");
 }

And here is the Tk script

 #!c:/perl/bin/perl -w
 use strict;
 use Tk;
 my $mw = MainWindow->new;
 my $fileName;
 FillWindow($mw, 'Choose Script to Run:',@ARGV);
 
 MainLoop;
 
 sub FillWindow 
 {
    my ($window, $header,@List) = @_;
    my @selected;
    my $i;
    $window->Label(-text => "$header")->pack;
    foreach (@List)
    {
      push @selected, 0;
      $mw->Checkbutton(-text=>"$_",-variable=>\$selected[$i])->pack;
      $i++;
    }
    $mw -> Button(-text=>"Select", -command =>sub
                 {
                    my @selectedOptions;
                    for (my $i=0; $i <= $#List; $i++)
                    {
                        if ($selected[$i])
                        {
                            push @selectedOptions, $List[$i];
                        }
                     }
                     print STDOUT join(" \n ", @selectedOptions);
                     $mw -> destroy;
 	         })->pack;	
   
 }