Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Tk DirSelect Buttons

by merrymonk (Hermit)
on Apr 19, 2018 at 09:08 UTC ( [id://1213155]=perlquestion: print w/replies, xml ) Need Help??

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

I want a resizable directory selection facility. It seems that DirSelect gives this and the code below gives the display I want.
use Tk; use strict "vars"; use Tk::DirSelect; my $mw; $mw = MainWindow->new; my $ds = $mw->DirSelect( -width => 100, -height => 25, -background => red ); my $dir = $ds->Show(); MainLoop;
There are ‘OK’ and ‘Cancel’ buttons at the bottom.
How do I associate subroutines with these button, so for example I can use the selected directory?

Additional questions are:
How can I change the text on these buttons?
How can I add or remove buttons at the bottom?

I suspect this is simple but I have failed to find an answer.

Replies are listed 'Best First'.
Re: Tk DirSelect Buttons
by pryrt (Abbot) on Apr 19, 2018 at 13:39 UTC

    The documentation at Tk::DirSelect is rather sparse, and does seem to be missing making this critical bit of information explicit (which is silly, because that's the whole point of the module). The module has no real tests, and no example scripts. The documentation does have one useful feature: it recommends trying Tk::chooseDirectory, "which uses native system dialogs where available" and comes with Tk starting in v804 (current version is 804.034). The chooseDirectory documentation has an example... I'd probably head down that route, if I were you.

    However, the variable naming in the DirSelect SYNOPSIS implies that $ds->Show() returns the directory. The following test script shows that's the case (and compares using DirSelect vs chooseDirectory).

    use warnings; use strict; use Tk; my $mw = MainWindow->new(); foreach my $fn ( \&use_chooseDirectory, \&use_DirSelect ) { my $dir = $fn->(); if (!defined $dir) { warn 'No directory selected'; } else { warn "Selected $dir"; } } sub use_chooseDirectory { my $dir = $mw->chooseDirectory(-initialdir => '~', -title => 'Choose a directory'); return $dir; } sub use_DirSelect { use Tk::DirSelect; my $ds = $mw->DirSelect(); my $dir = $ds->Show(); return $dir; }

    edit: sorry, forgot to say, this is just showing how to use the resulting directory: it's just a string returned from the Show method. You don't need to associate a method with it. Notice in my code that I never do the MainLoop. If I did, I would have hooked the call to use_DirSelect() to some button or control in the MainWindow (like 'File > Change Directory'); I would have done the same for use_chooseDirectory() as well. AFAICT, there's no real need to change the button text (and that would tend to confuse people, I think.

      Thank you. You were right $dir does give the value of the selected directory when OK is used and nothing when Cancel is used!
      I have been using chooseDirectory but the problem is that I cannot change its size and want it to be bigger.
        the problem is that I cannot change its size and want it to be bigger

        Why don't you try a different widget? Look at this example, it allows you to make the window and font as big as you want. Tk://DirTree

        #!/usr/bin/perl use warnings; use strict; use Tk; require Tk::DirTree; require Tk::Adjuster; require Tk::TList; # an example that has been floating around the monastery for years # The initial directory my $initial_dir = '/'; # The main window... my $main = new MainWindow( -title => 'Exploreeeeeer :o)' ); # A frame for the tree, adjuster and tlist my $tree_adj_tablist = $main->Frame(); $tree_adj_tablist->pack( -expand => 'yes', -fill => 'both', -side => 'top' ); # A scrolled directory tree my $tree = $tree_adj_tablist->Scrolled( 'DirTree', -width => 155, -height => 55, -scrollbars => 'osoe', -background => 'White', -selectmode => 'single', -selectbackground => 'DarkBlue', -selectforeground => 'White', -showhidden => 1, -directory => $initial_dir ); $tree->pack( -expand => 'yes', -fill => 'both', -padx => 2, -pady => 2, -side => 'left' ); # An adjuster my $adjuster = $tree_adj_tablist->Adjuster( -widget => $tree, -side => 'left' ); $adjuster->pack( -side => 'left', -fill => 'y' ); # A scrolled tab_list widget my $tab_list = $tree_adj_tablist->Scrolled( 'TList', -background => 'White', -orient => 'vertical', -selectmode => 'extended', -scrollbars => 'os' ); $tab_list->pack( -expand => 'yes', -fill => 'both', -padx => 2, -pady => 2, -side => 'right' ); # A Quit button (will be suppressed???...) my $quit = $main->Button( -text => 'Quit', -underline => 0, -width => 6, -command => sub { exit } ); $quit->pack( -side => 'right', -padx => 10, -pady => 10 ); # Configuring tree and tab_list widgets... $tree->configure( -browsecmd => sub { list_dir( $tab_list, @_ ); } ); # We list the content of the initial dir inside the tab_list list_dir( $tab_list, $initial_dir ); MainLoop(); #--------------------------------------------------------------------- +------- # Displays Dirs and files in TList widget sub list_dir { my ( $tab_list, $path ) = @_; # Erase the TList content $tab_list->delete( 0, 'end' ); opendir MY_DIR, $path or return; foreach my $file ( sort readdir(MY_DIR) ) { # Do not display '.' and '..' next if ( $file eq '.' or $file eq '..' ); # Insert the files in the TList $tab_list->insert( 'end', -text => $file ); } closedir MY_DIR; }

        I'm not really a human, but I play one on earth. ..... an animated JAPH
Re: Tk DirSelect Buttons
by Anonymous Monk on Apr 19, 2018 at 12:29 UTC
    Tk is Tk. Use Tk::WidgetDump. Try stuff. Modal dialog doesn't need hooks its modal dialog.
      Thank you. I used WidgetDump and got I 'display' that showed the OK button was in a tree of
      dirselect->frame1->button
      and the Cancel button was in a tree
      dirselect->frame1->button1

      I tried using the line
      $ds->frame1->button->configure(-command => [\&OK_cb]);
      to associate the sub OK_cb with the OK button. However, I got the error message

      Failed to AUTOLOAD 'Tk::DirSelect::frame1' at C:\radan-docs\radan-f\perlwork\programs\atest_x_select.pz line 14.

      pryrt suggestion has worked and let me get the selected directory but it would be good to know how to associate the sub OK_cb so that I can look at other things I may want to alter.

         $ds->frame1->button->configure

        What docs are you reading? Try ->Subwidget or ->children to find the window/object you're after

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1213155]
Approved by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-18 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found