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

I have to develop GUI interface. I am workin on perl . Therefore i decided to go with Tk module for GUI development. In back-end i have my .pl script for which i want to develop GUI ( I call it in perl usually as perl script.pl arguments -i (string) , -d (string), -o (numeric) ) . Now how to pass these 4 arguments in Tk module. I am beginner in GUI development. Therefore i am looking for some guidance and help , that will help me to go further and improve my skill.
  • Comment on Passing arguments for GUI development in Tk

Replies are listed 'Best First'.
Re: Passing arguments for GUI development in Tk
by choroba (Cardinal) on Sep 15, 2015 at 13:11 UTC
    Use Tk to populate the argument strings, add a button to the GUI to run the script. Let the button invoke the original script through system:
    #! /usr/bin/perl use warnings; use strict; use Tk; sub run { my ($mw, $option_i, $option_d, $option_o) = @_; if ($option_o =~ /^[0-9]+$/) { system 'script.pl', '-i', $option_i, '-d', $option_d, '-o', $option_o; } else { $mw->messageBox(-type => 'Ok', -title => 'Invalid option', -icon => 'error', -message => '-o must be numeric!'); } } my $mw = 'MainWindow'->new(-title => 'GUI'); my $mf = $mw->Frame->pack; my $fi = $mw->Frame->pack; my $label_i = $fi->Label(-text => '-i')->pack(-side => 'left'); my $entry_i = $fi->Entry->pack; my $fd = $mw->Frame->pack; my $label_d = $fd->Label(-text => '-d')->pack(-side => 'left'); my $entry_d = $fd->Entry->pack; my $fo = $mw->Frame->pack; my $label_o = $fo->Label(-text => '-o')->pack(-side => 'left'); my $entry_o = $fo->Entry->pack; my $bf = $mw->Frame->pack; my $run_b = $bf->Button(-text => 'Run', -foreground => 'green', -activeforeground => 'green', -command => sub { run($mw, map $_->ge +t, $entry_i, $entr +y_d, $entry_o) }, )->pack(-side => 'left'); my $cancel_b = $bf->Button(-text => 'Cancel', -command => sub { $mw->exit }, )->pack; MainLoop();
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Passing arguments for GUI development in Tk
by Athanasius (Archbishop) on Sep 15, 2015 at 13:03 UTC

    Hello archana12, and welcome to the Monastery!

    I don’t understand why you would want to pass the name of the Perl script to the GUI module? The other three options can just be handled in the normal way. For example, the following script combines example code from the synopses for Getopt::Long and Tk:

    #! perl use strict; use warnings; use Getopt::Long; use Tk; my ($i, $d, $o); GetOptions ("i=s" => \$i, "d=s" => \$d, "o=i" => \$o) or die "Error in command line arguments: $!"; my $mw = MainWindow->new; $mw->Label(-text => "$i #$o")->pack; $mw->Button ( -text => $d, -command => sub { exit }, )->pack; MainLoop;

    You could then call it like this (using wperl in place of perl to by-pass the console):

    >wperl 1374_SoPW.pl -o 42 -i Title -d Quit

    (If that doesn’t answer your question, please explain what you’re looking for in greater detail.)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Passing arguments for GUI development in Tk
by atcroft (Abbot) on Sep 15, 2015 at 18:24 UTC

    The sidebar entitled "Specifying arguments to callbacks" in Philipp Janert's article Using Advanced Widgets in Perl/Tk (IBM developerWorks) describes the difference in two methods of passing arguments to a -command option for a Tk widget-one fixing the options used at the time the constructor is first executed, the other taking the options at execution time. It may be worth your time to review.

    Example code to display the effect

    Hope that helps.

Re: Passing arguments for GUI development in Tk (preferred idiom)
by Discipulus (Canon) on Sep 16, 2015 at 07:44 UTC
    Hello archana12

    as Athanaisius told you, Getopt::Long is the best way to receive arguments for a Perl program. I want just add some words about your specific case. In fact, when you do the effort to use Getopt::Long to parse args, is worth to add at least a --help switch. Not only your program will be nicer with users but you can print the help or synopsis when parsing arguments fails instead of having a lonely (for example) Unknown option: g.

    Perl tends to become very idiomatic and here is my common idiom to parse args:
    unless ( GetOptions ( "example=s" => \$par_example, # string needed # more options a +nd switches.. "help" => \$par_help, # switch )) { my_show_help(); die "Error in command lin +e arguments: $!"} if (defined $par_help){my_show_help();exit;}
    Even more: you can profit of the synergy of combining Getopt::Long with Pod::Usage if (and you should) you embed POD documentation into your program. See the very good node The Dynamic Duo --or-- Holy Getopt::Long, Pod::UsageMan!.

    So the idiom can be expanded to include a pod2usage call if --manual is given.

    In your specific case, i.e. a Tk application, your program can also be called by a double click or a custom link with wrong parameters and in this case you'll have no time to read errors emitted by your wise error check. You can circumvent this with a simple wait_for_input sub and the result will be something like:
    unless ( GetOptions ( "example=s" => \$par_example, # string needed # more options a +nd switches.. "help" => \$par_help, # switch )) {pod2usage(-verbose => 1,-exitval => 'NOEXI +T'); &wait_for_input; exit 1;} if (defined $par_help){pod2usage(-verbose => 1,-exitval => 'NOEXIT'); +&wait_for_input; exit 0;} sub wait_for_input{ print "\n\nPress return when ready...\n"; while (<STDIN>){last if $_ } }


    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.