http://www.perlmonks.org?node_id=15422

Buckaroo Buddha has asked for the wisdom of the Perl Monks concerning the following question: (gui programming)

Because my perl application is going to be used by lusers, I'd like it to be runnable "standalone", with a graphical user interface.

One thing I need help with is simplifying the "choose a file" interface, for operations such as opening and saving files.

This program will be running only on Windows, if that helps. I think it might, since Windows has a native file browser functionality. However, I don't know how to get at these Windows native calls, such as GetOpenFileName or GetSaveasFileName (as they're known in VB).

Can anybody help with something like this?

Originally posted as a Categorized Question.

  • Comment on How do I make Open and Save As file dialogs? (Win32)

Replies are listed 'Best First'.
Re: How do I make Open and Save As file dialogs? (Win32)
by ase (Monk) on Jun 12, 2000 at 02:17 UTC

    While I haven't any experience with Win32::GUI, I've used Perl/Tk successfully for this exact problem. The key methods are getOpenFile and getSaveFile Here's a short example:

    #!/usr/bin/perl -w use Tk 8.0; use strict; my $mw = MainWindow->new( -title => 'File Test' ); my $menu_bar = $mw->Menu; $mw->configure( -menu => $menu_bar ); my $file_mb = $menu_bar->cascade( -label => '~File', -tearoff => 0 ); $file_mb->command( -label => 'Open', -underline => 0, -command => \&f_ +open ); $file_mb->command( -label => 'Save', -underline => 0, -command => \&f_ +save ); $file_mb->separator; $file_mb->command( -label => 'Exit', -underline => 1, -command => sub{ +exit} ); MainLoop; sub f_open { my $filename = $mw->getOpenFile( -title => 'Open File:', -defaultextension => '.txt', -initialdir => '.' ); ### do something with $filename warn "Opened $filename\n"; } sub f_save { my $filename = $mw->getSaveFile( -title => 'Save File:', -defaultextension => '.txt', -initialdir => '.' ); ### do something with $filename warn "Saved $filename\n"; } #

    Note that Question 16.2. - Is there a file selector? of the (somewhat stale) Perl/Tk FAQ lists several alternatives. Which, if any, of these is what got built into the Tk of ActiveState Perl, I don't know...

Re: How do I make Open and Save As file dialogs? (Win32)
by mdillon (Priest) on May 30, 2000 at 20:34 UTC

    In the ActivePerl FAQ: Is there a way to do GUI programming with ActivePerl?

    It refers to Win32::GUI, which seems to be the module's author's page; it also has a lot of useful info about other Win32 modules, as well as links to precompiled versions for the compiler impaired.

    Below is some (untested) code based on a message from the Win32::GUI mailing list:

    use Cwd; # returns selected file name; undef if Cancel sub file_open_dialog { my $file_spec = "*.bmp\0" . " " x 256; # init a C-friendly buffer my $dir = cwd; GUI::GetOpenFileName( -title => 'Load/Save File', -owner => $Win, -directory => $dir, -file => $file_spec, ) }
Re: How do I make Open and Save As file dialogs? (Win32)
by perlcgi (Hermit) on May 30, 2000 at 21:21 UTC

    Win32::GUI Installation Tip:

    If you don't have a C compiler to build the Win32::GUI module and you have ActivePerl, do:

    PPM install --location http://Jenda.Krynicky.cz/perl/ Win32-GUI
    at a command prompt.

Re: How do I open and save files in a GUI environment?
by perlcgi (Hermit) on May 31, 2000 at 21:23 UTC
    Check out this utility by Hydro Pretty cool, and some actual Win32::GUI code to get your teeth into.

    Originally posted as a Categorized Answer.

Re: How do I make Open and Save As file dialogs? (Win32)
by jdporter (Paladin) on Dec 21, 2006 at 22:11 UTC

    The Tk in ActiveState Perl includes a Tk::FileSelect module which provides a generic file selection dialog.

Re: How do I make Open and Save As file dialogs? (Win32)
by heatblazer (Scribe) on Apr 05, 2013 at 09:57 UTC

    A question: I can`t seem to open a filehandle in the functions for writing to a file?

    Opening for reading is OK, writing fails.