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

This is my code:

#!/usr/bin/perl use warnings; use Tk; use Tk::widgets; my $main = new MainWindow; $main->title('Tk_Test.pl'); $frame = $main->Frame(); $menubar = $main->Frame(-borderwidth=>2); $text = $frame->Text(-wrap=>"word", -height=>10, -font=>'Arial'); $filebutton = $menubar->Menubutton(-text=>"File"); $filemenu = $filebutton->Menu(-tearoff=>0); $filebutton->configure(-menu=>$filemenu); $filemenu->command(-command => \&print_frame, -label => "Print frame") +; $filemenu->command(-command => \&get_file, -label => "Get file"); $filemenu->command(-command => \&dir_dialog, -label => "Get dir"); # position the widgets $filebutton->pack(-side => 'left'); $menubar->pack(-side => 'top', -fill => 'x'); $frame->pack(-side => 'top', -fill => 'both'); $text->pack(-side => 'right', -fill => 'both'); $text->insert('1.0', "Write something here... Then click File/Print fr +ame\n"); MainLoop; sub print_frame { print $text->get("1.0", "end"); # prints frame text to STDOUT } sub get_file { $input_file = $main->getOpenFile(); $text->insert('1.0', "You opened $input_file\n"); print "You opened $input_file\n" if $input_file; } sub dir_dialog { my $w = shift; my $ent = shift; my $dir; $dir = $w->chooseDirectory; if (defined $dir and $dir ne '') { $ent->delete(0, 'end'); $ent->insert(0, $dir); $ent->xview('end'); } }

My problem is with sub dir_dialog. I got it from the widgets demo Common dialogs, Directory selection dialog. In the demo it works perfectly but I don't know how to connect it to my script. I'll appreciate some enlightenment.

Replies are listed 'Best First'.
Re: Need to browse directories
by wwe (Friar) on May 27, 2010 at 10:58 UTC
    Hi, you need to invoke the chooseDirectory method from an other widget like this: $dir = $main->chooseDirectory; You missed to pass needed arguments too the dir_dialog. If you change the line 18 to $filemenu->command(-command => sub{ dir_dialog($main) }, -label => "Get dir"); the dialog comes up. you also need to pass the second argument for $ent.
      Thanks. That's what I needed.
A reply falls below the community's threshold of quality. You may see it by logging in.