0
#!/opt/perl/bin/perl
use strict;
use Tk;
my $response= Mainwindow->new;
$response => $mw->messageBox(-title => 'Title', 
             -message => 'Message', 
             -type => 'YesNo', -icon => 'question', 
             -default => 'yes'); 

This simple perl produces/displays two dialog boxes, how do you prevent it?

Jens
  • 69,818
  • 15
  • 125
  • 179
Steve
  • 31
  • 4

1 Answers1

1

You have errors in the code you posted, so it will not compile.
From your question I understand, that you want to present a Dialog window, without opening a MainWindow at the same time. It is necessary for a Tk application to instantiate a Tk::MainWindow that serves as a root in the applications window hierarchy. However it is not required to actually display this window.

If you want to hide your MainWindow you call ->withdraw on it like so:

use strict;
use warnings;
use Tk;

my $mw= MainWindow->new;
$mw->withdraw;
my $response = $mw->messageBox(-title => 'Title', 
                               -message => 'Message', 
                               -type => 'YesNo', 
                               -icon => 'question', 
                               -default => 'yes'); 
print "$response\n";

Note that the messageBox call blocks until the user clicks a button. MessageBox calls into the event loop as needed, so you don't have to start the application by calling MainLoop()

clamp
  • 2,552
  • 1
  • 6
  • 16