Dialog boxes are generally used to do one of the following:

1.Display information to the user.

2.Get a simple response from the user (usually a 'yes' or 'no')

Based on that, the information you can get is related to which button they pressed.

If you want a callback sub associated with a button press, you will need to create your own top level window and use the callback methods described by the first poster.

Here is a sample I used for Perlbox because I was not pleased with DialogBox. In short, this is a simple reimplementation of DialogBox, but you could alter it to have the callback functions that you want.

The following code has been released under the GPL License

You could use this Dialog like the following 'calling code", and it would work just like a regular dialog:

my $text="Perlbox Voice ".PBOX_VERSION."\nDecember 2004\nby Sha +ne Mason\n(me\@perlbox.org)\nhttp://perlbox.org"; my $d=$mainwindow->pSimpleDialog(-title => "perlbox",-text =>$t +ext,-OKonly=>'yes'); my $button = $d->Show; if(!$button){ &myCANCEL; } else{ return; }

Or you could alter the module to serve your own (GPL'd, open source) purpose (call back a function in this module, or pass a more meaningful return to the calling code above). Here is the module itself.

package Perlbox::perlboxTK::pDialog::pSimplesDialog; use Tk; use strict; use vars qw( $VERSION @ISA) ; $VERSION = '0.1.x' ; require Tk::Toplevel ; @ISA = qw( Tk::Toplevel ) ; Construct Tk::Widget 'pSimpleDialog' ; my $mw; my ($textVar, $titleVar,$okonly); my $returnVal; return 1; sub Populate(){ my $args; ($mw, $args) = @_; $mw->SUPER::Populate($args); $textVar = delete $args->{"-text"}; $titleVar= delete $args->{"-title"}; $okonly = delete $args->{"-OKonly"}; $mw->protocol( 'WM_DELETE_WINDOW' => sub { } ) ; $mw->transient( $mw->MainWindow ) ; } sub Show(){ #construct the size of our widget my $ht=150; $mw->geometry("200x".$ht."+10+10"); $mw->title($titleVar); my $label=$mw->Label(-text=>$textVar, -wraplength=>190, -font= +>($mw->fontCreate(-family=>"courier",-size=>9)),-anchor=>'w',-justify +=>'left')-> place(-y=>10,-x=>10, -width=>190, -height=>100); if($okonly ne 'yes'){ my $cmdCancel=$mw->Button(-text=>"NO", -relief=>'flat',-com +mand=> sub{&myNo})-> place(-y=>$ht-30,-x=>10,-width=>80, -height=>20); } $mw->waitVariable( \$returnVal ) ; $mw->withdraw ; $returnVal; } sub myYes(){ $returnVal=1; } sub myNo(){ $returnVal=0; }
As you see, it has two buttons with their own callbacks and IS a top level widget itself. Also, not that this can have 'Yes' and a 'No' or an 'OK' only. Look at the Perlbox source code to get more examples on how to call.

In reply to Re: Understanding Tk::DialogBox by scmason
in thread Understanding Tk::DialogBox by rvosa

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.