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

Hello World!

Here's a simple script to show my problem. I am running ActivePerl 5.8.1.807, Tk 800.024, W2K.

use strict; use warnings; use Tk; my $mw = MainWindow -> new; my $button = $mw -> Button (-command => \&save, -text => "save") -> pa +ck;<br> MainLoop; sub save {my $file = $mw -> getSaveFile}
If you click on the save button, and select an existing file, a dialog box appears asking if you want to overwrite the file.

In my application, selecting an existing file implies that you want to append to that file, rather than overwrite it. As this dialog box is then inappropriate and confusing, I want to stop it from appearing. I have tried to do this, but have had no luck, so I was hoping someone would help me out.

I had a look through c:\perl\site\lib\Tk\FBox.pm and found mention of a -force option, but when I tried to use it,

sub save {my $file = $mw -> getSaveFile (-force => 1)}
I got the following error:

Tk::Error: unknown option "-force", must be -defaultextension, -filetypes, -initialdir, -initialfile, -parent or -title at C:/Perl/site/lib/Tk.pm line 276.
Tk callback for tk_getSaveFile
[\&main::load]
Tk callback for .button
Tk::__ANON__ at C:/Perl/site/lib/Tk.pm line 228
Tk::Button::butUp at C:/Perl/site/lib/Tk/Button.pm line 111
(command bound to event)

In FBox.pm sub Done, I found the area of code that checks whether the file exists, and causes the dialog box to appear. In my naivety, I assumed that commenting this section out would stop the dialog box from appearing, but it didn't.

So, what I would like to do, is to be able to use the -force option. Failing that I would like to have my own getSaveFile called getSaveFileNC which doesn't cause a popup if the file exists, and failing that, I would like to be able to comment that section of code out of FBox.pm, and have the change make a difference.

Unfortunately, I have no idea how to go about doing any of these things. Please help!

Thanks.

Spike.

janitored by ybiC: Balanced <code> tags around codeblock, and other wee tiny format tweaks for legibility

Replies are listed 'Best First'.
Re: How to change a Tk module?
by Anonymous Monk on Mar 17, 2004 at 12:29 UTC
    Tk::FBox is used only on Unix systems. On Windows the native dialog is used through the C interface (see pTk/mTk/win/tkWinDialog.c in the Tk distribution). Unfortunately it is not possible to change the behavior without recompiling.

    Maybe you can try Win32::FileOp instead? Or just use the Tk::FBox object directly, but this means that the dialog will look "different" than a standard Windows dialog.

      Thanks for the Win32::FileOp suggestion, it seems to do the trick. I have one last problem, now, and that is with positioning the SaveAsDialog window. It seems to always appear in the top left corner of the screen. I'm wondering if the handle option has anything to do with this? Have I set it correctly, if my main Tk window is called $mw?

      $out = SaveAsDialog (
      -title => "$title - Save your results as:",
      -filters => [
      'CSV files' => '*.csv',
      'All files' => '*.*'],
      -defaultfilter => 1,
      -handle => "$mw",
      );

      Thanks,
      Spike.

        The handle is probably a Win32 HWND. You can get the HWND by calling $mw->id (I'm not 100% sure, consult the Tk::Widget and Tk::Wm documentation).
Tk is not Tk::FBox!
by PodMaster (Abbot) on Mar 17, 2004 at 12:13 UTC
    You said
    I got the following error:

    Tk::Error: unknown option "-force", must be -defaultextension, -filetypes, -init ialdir, -initialfile, -parent or -title at C:/Perl/site/lib/Tk.pm line 276.

    You need to create a Tk::FBox object like outlined in the docs.

    update: Hmm, try any of

    and see what works. Did I mention I don't like Tk much? :)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.