in reply to Re^3: file open in perl tk
in thread file open in perl tk

You should learn to use code tags to help us read your code
<code> ....your code here </code>
but here is a working version, of what I THINK you want.
#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::FileSelect; my $mw = MainWindow->new; $mw->configure( -background => 'black', -foreground => 'white' ); $mw->geometry( "400x100" ); $mw->title( "Multiple Windows Test" ); my $button1 = $mw->Button( -text => "view Results", -background => "cyan", -command => \&button1_sub )->pack( -side => "right" ); $mw->Button( -text => "Exit", -command => sub { exit } ) ->pack( -side => "bottom" ); sub button1_sub { my $subwin1 = $mw->Toplevel; $subwin1->geometry( "500x400" ); $subwin1->title( "Sub Window #1" ); my $fh; open( $fh, '+<', "./$0" ) or die $!; #just open script for test my @contents = <$fh>; # print "@contents\n"; close( $fh ); my $sublable = $subwin1->Scrolled( 'Text', -scrollbars => 'osoe', )- +>pack; $sublable->insert( 'end', @contents ); my $subwin_button = $subwin1->Button( -text => "Close window", -command => sub {$subwin1 => 'destroy'} )->pack( -side => "bottom" ); #=================Creating save buttion on subwindow =========== my $save_button = $subwin1->Button(-text=>'save', -command =>\&get_save, -background =>'cyan')->pack(-side=>'right'); + } MainLoop; #sub get_save{ # getSaveFile(-title => 'Select a file to APPEND',-defaultextension=>' +.in'); #} sub get_save { my $dst = $mw->getSaveFile( -initialdir => $ENV{HOME}, -defaultextension => '.in', -title => 'Save', -filetypes => [ [ 'myfiles' => '.in' ], [ 'All files' => '*' ], ], ); $dst ||= '<undef>'; warn "dst=$dst"; }

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^5: file open in perl tk
by vr786 (Sexton) on Nov 03, 2010 at 10:13 UTC

    Thank you very much for helping me, one more issue , the using above mentioned code I am not able to "save" the changes which are made by the user , how can i save the changes ........

      You have 2 easy ways. One is to use a TextEdit widget, instead of a Text widget. The TextEdit will give you a Save and SaveAs entry in the File menu. Google for examples.

      To keep your basic design though, something like this code is what you want:

      #!/usr/bin/perl use warnings; use strict; use Tk; use Tk::FileSelect; my $mw = MainWindow->new; $mw->configure( -background => 'black', -foreground => 'white' ); $mw->geometry( "400x100" ); $mw->title( "Multiple Windows Test" ); my $button1 = $mw->Button( -text => "view Results", -background => "cyan", -command => \&button1_sub )->pack( -side => "right" ); $mw->Button( -text => "Exit", -command => sub { exit } ) ->pack( -side => "bottom" ); sub button1_sub { my $subwin1 = $mw->Toplevel; $subwin1->geometry( "500x400" ); $subwin1->title( "Sub Window #1" ); my $fh; open( $fh, '+<', "./test.txt" ) or die $!; my @contents = <$fh>; # print "@contents\n"; close( $fh ); my $sublable = $subwin1->Scrolled( 'Text', -scrollbars => 'osoe', )- +>pack; $sublable->insert( 'end', @contents ); my $subwin_button = $subwin1->Button( -text => "Close window", -command => sub {$subwin1 => 'destroy'} )->pack( -side => "bottom" ); #=================Creating save buttion on subwindow =========== my $save_button = $subwin1->Button(-text=>'save', -background =>'cyan', -command => sub{ my $dst = $mw->getSaveFile( -initialdir => $ENV{HOME}, -defaultextension => '.in', -title => 'Save', -filetypes => [ [ 'myfiles' => '.in' ], [ 'All files' => '*' ], ], ); $dst ||= "$0.edit"; #in case user hits cancel warn "dst=$dst"; open (my $fh, '>',$dst) or die "$!\n"; print $fh $sublable->get("1.0","end"); } )->pack(-side=>'right'); } MainLoop;

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

        Hi monk, I found a problem using above code , whenever I try to open a file "./test.txt" it will show the alternative lines only, if my test.txt file containing the data 10 lines it will show only five lines , why it is behaving can you help , how can i over come this problem?