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

I am having trouble with the -initialdir option of getOpenFile.
The situation was that is was working on my system but not at one of my user’s.
I have written a test application where I used the code for getOpenFile from the proper application.
This is below.
I now have the problem that the –initialdir option does not work in the test code!
In the test you use the first button to select a directory.
This is shown in the entry box and can be altered.
You then use the second button to try getOpenFile.
If the directory cannot be found you get an error message.
In getOpenFile you can see what directory is acting as the ‘start’ directory.
I would be grateful if anyone can tell me why this test code does not go to the correct directory and/or why I find different behaviour on different systems
# test-dir # this is to test the initialdir option for the file browser use strict "vars"; # say to use Tk module use Tk; use Tk::Dialog; my ($mw); my ($sel_dir); my ($add2_entry); my ($dir_full); # create the screen menu # create main window $mw = MainWindow->new; $mw->title("initialdir test"); # button to select the directory $mw->Button(-text => "Select Directory", -command => \&setdir_cb)->gri +d(-row=>0, -column=>0); $mw->Label(-text => "Selected Directory")->grid(-row=>1, -column=>0); $add2_entry = $mw->Entry(-width => 60) ->grid(-row=>1, -column=>1); $dir_full = "No directory selected"; $add2_entry->configure( -textvariable => $dir_full); # button to use the file broswer $mw->Button(-text => "Try File Broswer", -command => \&open_file_brosw +er)->grid(-row=>2, -column=>0); MainLoop; #===================================== # # setdir_cb # # this invokes direcotry selection # #===================================== sub setdir_cb() { my ($err_message, $err_dialog); print "[setdir_cb] entry\n"; $dir_full = $mw->chooseDirectory( ); print "\n\n[setdir_cb] choosen directory <$dir_full>\n"; if(length($dir_full) == 0) { $mw->DialogBox(-title => 'No Directory Selected', -buttons =>['Ok' +], -default_button => 'Ok')->Show(); } else { $add2_entry->configure( -textvariable => $dir_full); $mw->update; } } #===================================== # # open_file_broswer # # this opens the file broswer # #===================================== sub open_file_broswer() { my ($file_selected, $title_str, $answer, $data_file_file_types, $data_ +file_title_str, $file_str); $dir_full = $add2_entry->cget( -textvariable); if(-d $dir_full) { $data_file_title_str = 'Test directory' ; $file_str = 'Data File'; $data_file_file_types = [ [$file_str,'.txt'], ['All files', '*'] ]; $file_selected = $mw->getOpenFile(-initialdir => $dir_full, -filetypes => $data_file_file_types, -title => $data_file_title_str ); } else { $title_str = "Directory <" . $dir_full . '> not found'; $answer = $mw->Dialog(-title => "Directory Not Found", -text => $title_str, -default_button => 'OK', -buttons => [OK], -bitmap => 'error' ) ->Show(); } }

Replies are listed 'Best First'.
Re: -initialdir option problemwith getOpenDir (perl tk)
by Anonymous Monk on Sep 26, 2008 at 10:53 UTC
    It seems to have to do with underlying operating systems idea of paths. For me, chooseDirectory returns paths with forward slashes, but -initialdir wants them with backward slashes. Tk.pm apparently has a built-in conversion for this but its not working ? I dunno, this works for me
    #!/usr/bin/perl -- use strict; use warnings; # test-dir # this is to test the initialdir option for the file browser use strict "vars"; # say to use Tk module use Tk; use Tk::Dialog; my ($mw); my ($sel_dir); my ($add2_entry); my ($dir_full); # create the screen menu # create main window $mw = MainWindow->new; $mw->title("initialdir test"); # button to select the directory $mw->Button( -text => "Select Directory", -command => \&setdir_cb ) ->grid( -row => 0, -column => 0 ); $mw->Label( -text => "Selected Directory" )->grid( -row => 1, -column +=> 0 ); $add2_entry = $mw->Entry( -width => 60 )->grid( -row => 1, -column => +1 ); $dir_full = "No directory selected"; $add2_entry->configure( -textvariable => $dir_full ); # button to use the file broswer $mw->Button( -text => "Try File Broswer", -command => \&open_file_bros +wer ) ->grid( -row => 2, -column => 0 ); MainLoop; #===================================== # # setdir_cb # # this invokes direcotry selection # #===================================== sub setdir_cb() { my ( $err_message, $err_dialog ); print "[setdir_cb] entry\n"; use File::Spec; $dir_full = File::Spec->canonpath( $mw->chooseDirectory() ); print "\n\n[setdir_cb] choosen directory <$dir_full>\n"; if ( length($dir_full) == 0 ) { $mw->DialogBox( -title => 'No Directory Selected', -buttons => ['Ok'], -default_button => 'Ok' )->Show(); } else { $add2_entry->configure( -textvariable => $dir_full ); $mw->update; } } #===================================== # # open_file_broswer # # this opens the file broswer # #===================================== sub open_file_broswer() { my ( $file_selected, $title_str, $answer, $data_file_file_types, $data_file_title_str, $file_str ); $dir_full = $add2_entry->cget( -textvariable ); if ( -d $dir_full ) { $data_file_title_str = 'Test directory'; $file_str = 'Data File'; $data_file_file_types = [ [ $file_str, '.txt' ], [ 'All files' +, '*' ] ]; warn "-initialdir => $dir_full,"; $file_selected = $mw->getOpenFile( -initialdir => $dir_full, -filetypes => $data_file_file_types, -title => $data_file_title_str ); } else { $title_str = "Directory <" . $dir_full . '> not found'; $answer = $mw->Dialog( -title => "Directory Not Found", -text => $title_str, -default_button => 'OK', -buttons => ['OK'], -bitmap => 'error' )->Show(); } }
      Very many thanks for that golden nugget!
      It works for me as well and suspect I would never have thought of it.