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

Hi everyone How are things?

I need some help, I am sure it is simple but I can't seem to get er done.

I am trying to open a dialog select a file (works) and place/display it in the entry textbox for processing (doesn't work)

Any help would e greatly appreciated

#!/usr/bin/perl -w use strict; use Tk; my $ImageFilePath; my $mw = MainWindow -> new; $mw -> title("IMU Data Processing"); # sizes window $mw -> geometry('400x200'); #my $frm = $mw -> Frame(); # label for Image times file my $lblImageFile = $mw->Label(-text => "Image File:", -padx=>5, -pady= +>5); # entry textbox for Image times dialog my $entImageFile = $mw->Entry(-width=>30, -textvariable=>\$ImageFilePa +th); # button to browse for file my $butImageFile = $mw->Button(-text=>'Browse', -command=> sub { openI +mageFileDialog($entImageFile)}); MainLoop; sub openImageFileDialog { my $imageFilePathOpen = $mw->getOpenFile(-title => 'Open Image File:') +; }

Replies are listed 'Best First'.
Re: Placing a file path in a entrybox from a dialogbox
by stefbv (Priest) on Dec 02, 2009 at 16:20 UTC

    Something like this:

    #!/usr/bin/perl use strict; use warnings; use Tk; my $ImageFilePath; my $mw = MainWindow->new; $mw->title("IMU Data Processing"); # sizes window $mw->geometry('400x200'); #my $frm = $mw -> Frame(); # label for Image times file my $lblImageFile = $mw->Label( -text => 'Image File:', -padx => 5, -pady=>5, )->pack; # entry textbox for Image times dialog my $entImageFile = $mw->Entry( -width => 30, -textvariable => \$ImageFilePath, )->pack; # button to browse for file my $butImageFile = $mw->Button( -text =>'Browse', -command => sub { my $file = openImageFileDialog($entImageFile); $entImageFile->delete(0, 'end'); $entImageFile->insert(0, $file); } )->pack; MainLoop; sub openImageFileDialog { my $imageFilePathOpen = $mw->getOpenFile(-title => 'Open Image Fil +e:'); return $imageFilePathOpen; }
Re: Placing a file path in a entrybox from a dialogbox
by keszler (Priest) on Dec 02, 2009 at 15:41 UTC
    Untested!
    # change my $butImageFile = $mw->Button(-text=>'Browse', -command=> sub { openI +mageFileDialog($entImageFile)}); # to my $butImageFile = $mw->Button( -text=>'Browse', -command=> sub { $ImageFilePath = $mw->getOpenFile(-title => 'Open Image File:'); } );
      This solution will work fine. A textvariable can be used just like any other variable. If you change it, the display is automatically updated (no need for "edit" commands).

      As a small enhancement, I show how to use the "filetypes" filter. If the op wants to pass a ref to the textvariable, I show how to do that also although if this is a special purpose routine, I would have no problem with just accessing the $ImageFilePath directly without going through a reference. I also tend not to declare var names that aren't used again (like the LHS of button creation).

      # entry textbox for Image times dialog my $entImageFile = $mw->Entry(-width=>30, -textvariable=>\$ImageFilePath,)->pack() +; # button to browse for file $mw->Button( -text=>'Browse', -command=> sub { openImageFileDialog(\$ImageFilePath)}, )->pack(); MainLoop; sub openImageFileDialog { my $path_ref = shift; my @types = (["Image Files", [qw/.bmp .giff .tiff/]], ["All files", '*'], ); $$path_ref = $mw->getOpenFile(-title => 'Open Image File:', -filetypes => \@types); }