loztop has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have created a text to pdf converter. I would like to add a button which lets someone select a file rather than having to type the file name. Any ideas ?

Thank you very much, loztop.
#!/usr/bin/perl use Tk; use PDF::Create; use strict; use warnings; my $lab; my $address = "Enter file name"; my $window = MainWindow->new; $lab = $window->Label(-text => "Hello !")->pack; $window->Entry(-textvariable => \$address )->pack; $window->Button(-text => "Convert to pdf", -command => \&convert )->pa +ck; $window->Button(-text => "Quit", -command => \&finito )->pack; MainLoop; sub convert { my $height = 842; my $width = 595; my $result = index($address, '.'); my $a = substr($address, 0, $result); $a = $a.'.pdf'; my $pdf = new PDF::Create('filename' => $a ); my $root = $pdf->new_page('MediaBox' => [ 0, 0, $width, $height ]) +; # Add a page which inherits its attributes from $root my $page = $root->new_page; # Prepare font my $f1 = $pdf->font('Subtype' => 'Type1', 'Encoding' => 'WinAnsiEncoding', 'BaseFont' => 'Helvetica'); my $vert = $height -12; my $FilePath = "access.txt"; open(FILE, $address); while (my $line = <FILE>) { $page->string($f1, 10, 10,$vert, $line); $vert = $vert-12; if($vert<20){ $page = $root->new_page; $vert = $height -12; } } close FILE; $pdf->close; $lab -> configure(-text => "Congratulations $a has been created !" +); $address = "Convert another one !" } sub finito{ exit; }

Replies are listed 'Best First'.
Re: Perl Gui File browser select files
by Sinistral (Monsignor) on Jul 05, 2009 at 18:49 UTC
Re: Perl Gui File browser select files
by Marshall (Canon) on Jul 06, 2009 at 06:03 UTC
    You may just need getOpenFile() which will autoload with use Tk; See below for an example:
    #!/usr/bin/perl -w use strict; use Tk; my $current_file=(); our $mw = MainWindow->new; $mw->configure(-title=> "some title"); $mw->geometry("1000x400+0+0"); my $menu_f = $mw->Frame()->pack(-side=>'top',-fill=>'x'); my $menu_file = $menu_f->Menubutton (-text=>'File',-tearoff=>'false') ->pack(-side=>'left'); $menu_file->command(-label=>'Open', -command=> \&open_pdf); sub open_pdf { my @types = (["PDF files", [qw/.PDF /]], ["All files", '*'], ); $current_file= $mw->getOpenFile(-filetypes => \@types); print "$current_file\n"; } MainLoop();
    Oh, oops don't need any "our" variables here... this was a quick hack from code a few years ago. But this runs with Perl 5.6/5.8/5.10 on Windows...should be ok on *nix also.

    Update

    there are some things that I would change if I wrote the code now, like:

    -command=> \&open_pdf ## better -command=> open_pdf(..params...)
    But at the end of the day, it is 2 lines of code to get a Tk GUI dialog up that will select a file.
Re: Perl Gui File browser select files
by missingthepoint (Friar) on Jul 06, 2009 at 11:45 UTC

    Welcome to the monastery, loztop! :)

    I hope you enjoy your stay, however long it may be. Thankyou for posting your code; that's helpful. In future posts you can wrap it in tags like so:

    <c> ...code... </c>

    ... and it will be formatted nicely.

    Good to have you here. :)


    It is easy to identify flaws in others' code, but it is not noble. Nobility is gently, patiently, carefully suggesting improvements, leading others toward better programming.