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

In reply to Re: -initialdir option problemwith getOpenDir (perl tk) by Anonymous Monk
in thread -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.