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

Dear Monks,

I have been working on a code for getting some information from the user using GUI and also generating a text file, which I need to manipulate later. What I want to do is for the user to enter the file at the file entry location. What I am looking for is how to add a button at the side of the file name entry location, so that the user can either enter the location of the file or use the button to open a file dialog box. Once the user clicks and opens the dialog box to enter the file information, I want the user to also be able to see the file location in the entry text location for the file entry. Could you kindly help me with this.

#!/usr/local/bin/perl use Tk; #Global Variables my $bins = 10; my $process = "180nm"; # Main Window my $mw = new MainWindow; #GUI Building Area: User specifies file location my $frm_name = $mw -> Frame(); my $lab = $frm_name -> Label(-text=>"File Name:"); my $ent = $frm_name -> Entry(); #Number of bins my $scl = $mw -> Scale(-label=>"Number of bins :", -orient=>'h', -digit=>1, -from=>10, -to=>100, -variable=>\$bins, -tickinterval=>25); #Select the process my $frm_process = $mw -> Frame(); my $lbl_process = $frm_process -> Label(-text=>"Process "); my $rdb_40 = $frm_process -> Radiobutton(-text=>"40nm:GF", -value=>"40nm", -variable=>\$process); my $rdb_65 = $frm_process -> Radiobutton(-text=>"65nm:GF", -value=>"65nm",-variable=>\$process); my $rdb_140 = $frm_process -> Radiobutton(-text=>"140nm:NXP", -value=>"140nm", -variable=>\$process); my $rdb_180 = $frm_process -> Radiobutton(-text=>"180nm:GF", -value=>"180nm",-variable=>\$process); my $but = $mw -> Button(-text=>"Push Me", -command =>\&push_button); #Text Area my $textarea = $mw -> Frame(); my $txt = $textarea -> Text(-width=>40, -height=>10); my $srl_y = $textarea -> Scrollbar(-orient=>'v',-command=>[yview => $t +xt]); my $srl_x = $textarea -> Scrollbar(-orient=>'h',-command=>[xview => $t +xt]); $txt -> configure(-yscrollcommand=>['set', $srl_y], -xscrollcommand=>['set',$srl_x]); #Geometry Management $lab -> grid(-row=>1,-column=>1); $ent -> grid(-row=>1,-column=>2); $scl -> grid(-row=>2,-column=>1, -ipadx =>100); $frm_name -> grid(-row=>1,-column=>1,-columnspan=>2); $lbl_process -> grid(-row=>1,-column=>1); $rdb_40 -> grid(-row=>1,-column=>2); $rdb_65 -> grid(-row=>1,-column=>3); $rdb_140 -> grid(-row=>1,-column=>4); $rdb_180 -> grid(-row=>1,-column=>5); $frm_process -> grid(-row=>3,-column=>1,-columnspan=>2); $but -> grid(-row=>4,-column=>1,-columnspan=>2); $txt -> grid(-row=>1,-column=>1); $srl_y -> grid(-row=>1,-column=>2,-sticky=>"ns"); $srl_x -> grid(-row=>2,-column=>1,-sticky=>"ew"); $textarea -> grid(-row=>5,-column=>1,-columnspan=>2); MainLoop; ## Functions #This function will be executed when the button is pushed sub push_button { my $name = $ent -> get(); open(my $in0, '>', 'input.txt') or die "Cannot open input.txt file: $!. You might need to run the +script from a location where you have write access"; print $in0 $name, "\n"; print $in0 $process, "\n"; print $in0 $bins, "\n"; close $in0; $txt -> insert('end',"Process is $process. File location is $name +and selected bins is $bins.\n"); }
Here is the code I found for searching a file for open.
#!/usr/bin/perl -w use strict; use Tk; my $mw = MainWindow->new; $mw->geometry("400x400+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=> \&get_file); #other menu buttons shown here as examples ... #$menu_file->command(-label=>'Save', # -command=> \&save_log); #$menu_file->command(-label=>'Close', # -command=> \&close_log); #$menu_file->command(-label=>'Exit',-command=>\&exit_msg); sub get_file { my @types = (["Log files", [qw/.txt .log/]], ["All files", '*'], ); my $filepath = $mw->getOpenFile(-filetypes => \@types) or return(); print "$filepath selected\n"; return($filepath); } MainLoop;
thanks, regards, Ganesh.