Studio:Can I use Perl's Tk package in STRIDE?: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
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. | 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. | 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; | |||
} | |||
[[Category:Scripting]] | [[Category:Scripting]] |
Revision as of 20:35, 11 December 2007
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; }