Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

( #15422=categorized question: print w/ replies, xml ) Need Help??
Contributed by Buckaroo Buddha on May 30, 2000 at 19:46 UTC
Q&A  > GUI Programming


Description:

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?

Answer: How do I make Open and Save As file dialogs? (Win32)
contributed by ase

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...

Answer: How do I make Open and Save As file dialogs? (Win32)
contributed by mdillon

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, ) }
Answer: How do I make Open and Save As file dialogs? (Win32)
contributed by perlcgi

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.

Answer: How do I make Open and Save As file dialogs? (Win32)
contributed by jdporter

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

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (14)
    As of 2014-10-24 13:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (132 votes), past polls