button to browse the files on my local drive

button to browse the files on my local drive

Post by peretz.eya » Mon, 18 Sep 2006 19:02:20


i can't finf a solution to make a button which shows file browser on my
local drive.

thanks in advance
Eyal
 
 
 

button to browse the files on my local drive

Post by zentar » Mon, 18 Sep 2006 20:44:13

n 17 Sep 2006 03:02:20 -0700, XXXX@XXXXX.COM wrote:


Here is a nifty one posted awhile ago by someone, which I modified
to give you an Ok button. I print the selected file(s) in the Ok button
callback, but you could push them into an array or hash.

All you need to do, is make a button to launch this code.

#!/usr/bin/perl
use strict;
use Tk;
require Tk::DirTree;
require Tk::Adjuster;
require Tk::TList;

# The initial directory
my $initial_dir = '/';

# The main window...
my $main = new MainWindow( -title => 'Explorer)' );

# 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 => 35,
-height => 25,
-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'
);


# Ok button
my $ok = $main->Button(
-text => 'Ok',
-underline => 0,
-width => 4,
-command => sub { my $selected = $tab_list->info('selection');
#print "@{$selected}\n";
foreach( @{$selected} ){
print $tab_list->entrycget($_, '-text'),"\n";
}
})->pack( -side => 'right', -padx => 10, -pady => 10 );


# 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;
}
__END__


--
I'm not really a human, but I play one on earth.
http
 
 
 

button to browse the files on my local drive

Post by Rob Seege » Tue, 19 Sep 2006 11:27:23

Do you mean something like this:

use Tk;

my $mw = MainWindow->new(-title => "Select File");

my $filename;
my $entry = $mw->Entry(
-width => 40,
-textvariable => \$filename
)->grid(
$mw->Button(
-text => "...",
-command => sub {
$filename = $mw->getOpenFile(-title => 'File Browser');
}
),
);

MainLoop;
 
 
 

button to browse the files on my local drive

Post by mem » Tue, 19 Sep 2006 16:53:09


- code -

my $file_select_button = $mw -> Button(
-text=>"File Select",
-command =>\&dir_dialog
) -> pack;

sub file_select {
my $file = $main::mw->getOpenFile(-title => 'File Browser');

return $file;
}

- end of code -

the lil sub rotuine above will return selected file or nothing if the
user cancels :)
 
 
 

button to browse the files on my local drive

Post by EZP » Thu, 21 Sep 2006 13:49:50


Hello all,

Thank you very much for your creative and educated solutions,

Every one of you is a real expert.

Thanks again,

EZP
 
 
 

button to browse the files on my local drive

Post by mem » Thu, 21 Sep 2006 23:32:14


awww :) glad we could help.