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(); } }

In reply to -initialdir option problemwith getOpenDir (perl tk) by merrymonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.