in reply to Re^8: Perl tk - How to integrate external scripts
in thread Perl tk - How to integrate external scripts

Always use strict and warnings.  With that, you'd have gotten warnings like Variable "$open" will not stay shared, which might have been a hint that there's still something wrong with the scoping of the variable $open.

The problem is in particular that you've declared $open locally to the callback function push_button, and that you've declared the other callback functions locally to that function (which I had only realized after having indented your code reasonably...).   Perl does not have local subroutines, and if you declare named subs within other subs, they're essentially parsed as if they were declared top-level. Hence the scope issue.

Here's a stripped down version (with "minor" edits to make it actually runnable...), which uses real global variables for $open and $te (for which the same issues apply), so that they can be shared across various callback routines.

#!/usr/bin/perl -w use strict; use Tk; my $open; my $te; # Entry Window my $mw = new MainWindow; # The START button: my $button = $mw -> Button(-text => "Start",-command=>\&push_button)-> +pack(); MainLoop; sub push_button { my $mw = new MainWindow; $mw -> geometry ("1200x600"); # Menu bar $mw->configure(-menu => my $menubar = $mw->Menu, -background => 'white'); my $file = $menubar->cascade(-label => '~File'); my $edit = $menubar->cascade(-label => '~Edit'); my $help = $menubar->cascade(-label => '~Help'); $mw-> Button (-text =>'Open', -command =>\&open_file)->place(-x=>2 +40, -y=>35); $mw-> Button (-text =>'Get Statistics', -command =>\&get_statistic +s)->place(-x=>320, -y=>35); $te = $mw->Scrolled('TextUndo')->place(-x=>240, -y=>70); MainLoop; } sub open_file { $open = $mw->getOpenFile( #-filetypes => $types_OPEN, -defaultextension => '.sff' ); $te->Load($open); } sub get_statistics { my $fastafile= $open; print STDERR "selected file: $fastafile\n"; }

A few more comments:

Replies are listed 'Best First'.
Re^10: Perl tk - How to integrate external scripts
by Giorgio C (Novice) on Jan 31, 2012 at 14:40 UTC
    I do not really know how to thank you, you have been very kind ! Now all works ! My apologies for not posting my question well. It's my first programming experience so i I have a lot to learn. Heartfelt thanks for all your help !! Cheers, Giorgio
Re^10: Perl tk - How to integrate external scripts
by Giorgio C (Novice) on Feb 01, 2012 at 11:35 UTC
    Hi Eliya, following all your directions i corrected the script code (with the help of 'warnings' and 'strict'), now i have no much error messages coming out. Further, i was able to integreate inside the script of %statistics without problems (thanks to you surely)! Do you know if it's possible to print the result in a window ? (messagebox, or a little scrolled window, etc..). For now the result is printed out in the terminal and i would like to see it in a GUI window.
    #all global variable set well ! ........ $mw-> Button (-text =>'Open', -command =>\&open_file)->place(-x=>240, +-y=>35); $mw-> Button (-text =>'Get Statistics', -command =>\&get_statistics)-> +place(-x=>320, -y=>35); sub open_file {$open = $mw->getOpenFile( -filetypes => $types_OPEN, -defaultextension => '.sff'); $te->Load( "$open");} sub get_statistics { my $fastafile= $open; open (FASTA, "<$fastafile"); $/ = ">"; my $junkFirstOne = <FASTA>; while (<FASTA>) { chomp; my ($def,@seqlines) = split /\n/, $_; my $seq = join '', @seqlines; $stat->add_data(length($seq)); } %distrib = $stat->frequency_distribution(\@bins); print "Total reads:\t" . $stat->count() . "\n"; print "Total nt:\t" . $stat->sum() . "\n"; print "Mean length:\t" . $stat->mean() . "\n"; print "Median length:\t" . $stat->median() . "\n"; print "Mode length:\t" . $stat->mode() . "\n"; print "Max length:\t" . $stat->max() . "\n"; print "Min length:\t" . $stat->min() . "\n"; print "Length\t# Seqs\n"; foreach (sort {$a <=> $b} keys %distrib) { print "$_\t$distrib{$_}\n";}}; #This is the result in the terminal for an example $open file: Total reads: 4981 Total nt: 104724 Mean length: 21.024693836579 Median length: 21 Mode length: 21 Max length: 27 Min length: 18 Length # Seqs 18 290 19 456 20 874 21 1974 22 626 23 281 24 373 25 69 26 32 27 6 28 0
    It works fine and correctly. Do you know how to print this result in a GUI windows and not only in the terminal? Thanks you !!!
        Yes i see it it was a post started by me. I'v tried to set at the end :
        my $te2= tkinit->Scrolled( q/TextUndo/,-scrollbars => 'osw', -background => 'white', -font =>'normal', -wrap => 'none',)->pack; $te2>Load( "$distrib" ); MainLoop;
        But in this case there is a multiple print line to see on screen and however i am unable to see even one of them. Any suggestions?