Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Displaying text file with perl/tk

by akm2 (Scribe)
on Jul 30, 2001 at 17:03 UTC ( #100844=perlquestion: print w/ replies, xml ) Need Help??
akm2 has asked for the wisdom of the Perl Monks concerning the following question:

Below is a snippit of code I found to display text in a Tk Window.
sub show_instr { my $mw = shift; my $tl = $mw->Toplevel(-title => 'TkSolitaire Instructions'); my $text = $tl->Scrolled('ROText', -scrollbars => 'e', -wrap => 'word', -width => 80, -height => 32, -font => $FONT, -setgrid => 1, )->pack(-expand => 1, -fill => 'both'); $text->tagConfigure('title', -font => '-*-Helvetica-Bold-R-Normal--*-190-*-*-*-*-*-*', ); # separate paragraphs with two newlines my $instr = <<'EOInstr'; This program is free software. You may distribute or modify it under the terms of the Artistic license that is distributed with the Perl Kit. The card images in the cards directory came from <URL:http://www.waste.org/~oxymoron/cards/> and are distributed under the terms of the GPL. The object of the game is to pick up all the cards. You start with yo +ur base card (centered at the bottom). You may pick up (by clicking) any uncovered card whose denomination is one away from the base card's denomination (the ace is adjacent to both the king and the deuce). Su +it does not matter. Attempting to illegally pick up a card (if the card +is covered or if the card's denomination is not adjacent to the base card +'s denomination) will result in a terminal bell. Once you pick up a card +, it becomes the new base card. If you there are no more cards to pick up, click the base card. This +is equivalent to pulling the next card from the bottom of the pile. When the pile is exhausted, a card will be placed face down at the base. T +he game is over when no more cards are left in the field or when the base is exhausted and no legal moves remain. There is an undo option that lets you undo your last pickup. You can' +t undo once you've flipped the next base card because that would give an unfair advantage. My good friends Susan, Sarah and Mackenzie taught me how to play this game. I instantly loved it. Thanks for all the fun guys! When I relized my mom would soon be 29 and holding (again), I decided +that I'd let Tk and Fisher-Yates do the layout and shuffling so she (my mom +) could try her hand at this version of Solitaire. Happy 29th Birthday Mom! :) EOInstr chomp $instr; # make the paragraphs into one long line $instr =~ s/\n(?!\n)/ /g; $text->insert('end', "TkSolitaire Instructions\n", 'title'); $text->insert('end', $instr); }

if I wanted the window body to be the contents of a text file what would I need to do? Sorry, for such a dumb question but I cant figure this out.

Edit Masem 2001-07-31 - Title change from "Tk perl"

Comment on Displaying text file with perl/tk
Download Code
Re: Tk perl
by physi (Friar) on Jul 30, 2001 at 17:15 UTC
    put your file in an array and tail it to the text widget:
    open FILE, "filename" my @file = <FILE>; for my $line (@file) { $text->insert('end', $line); }
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
Re: Tk perl
by Jouke (Curate) on Jul 30, 2001 at 17:24 UTC
    In addition to physi's answer you should delete the part that starts with
    # separate paragraphs with two newlines my $instr = <<'EOInstr';
    and ends with

    $text->insert('end', "TkSolitaire Instructions\n", 'title'); $text->insert('end', $instr);
    Otherwise your text AND the current text will be displayed. You should replace the part I mentioned with physi's code Jouke Visser, Perl 'Adept'
    Using Perl to help the disabled: pVoice and pStory
Re: Tk perl
by IDStewart (Acolyte) on Jul 30, 2001 at 18:03 UTC
    When I relized my mom would soon be 29 and holding (again), I decided that I'd let Tk and Fisher-Yates do the layout and shuffling so she (my mom) could try her hand at this version of Solitaire.

    Oh man! Why do I suddenly feel old?

Re: Tk perl
by Jouke (Curate) on Jul 30, 2001 at 18:48 UTC
    This piece of code does the job for you:
    #!/usr/bin/perl -w use strict; use Tk; #First create a new mainwindow my $mainwindow = new Tk::MainWindow(); #Then create a textwidget (called 'Scrolled', with subtype 'Text") my $text = $mainwindow->Scrolled("Text", -scrollbars => 'se')->pack(-e +xpand => 1, -fill => 'both'); # Open the file open(F, "<$ARGV[0]") || die "can't open $ARGV[0]: $!\n\n"; while(<F>) { # insert each line to the $text widget $text->insert('end', $_); } # close the file close(F); # create a closebutton my $button = $mainwindow->Button(-text => "close", -command => sub {$m +ainwindow->destroy()})->pack(); # start the main loop! MainLoop;


    Jouke Visser, Perl 'Adept'
    Using Perl to help the disabled: pVoice and pStory

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://100844]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-12-25 22:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls