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

Oh monks, I am puzzled.

Also, I have a question about Tk::DialogBox. Here's some code:
sub configure { my $config = $mw->DialogBox( -title=>"Configure", -command=>\&done_config, -buttons=>["Apply","Done"] ); # other stuff, widgets that go in the top frame $config->Show(); }
So I'm making a dialog box, with two "bottom frame" buttons (that read "Apply" and "Done") and some other stuff in the top frame (entry fields for configuration values). Here's my question: how do I make the bottom frame buttons do something? How do I access them? The pod says something about B_"button name" but I don't understand what they mean. All I want is for a \&sub to be called when I press "apply".

Many thanks for any and all replies.

Edited by Arunbear: Changed title from 'Perl/Tk beginner question', as per Monastery guidelines

Replies are listed 'Best First'.
Re: Understanding Tk::DialogBox
by Velaki (Chaplain) on May 11, 2005 at 18:47 UTC

    What you are looking to do is create callback functions. These are subroutines that are executed when the button is pressed.

    For example:

    my $button1 = $mw->Button(-text => 'Quit', -command => \&exit);
    See how the function exit is the command that is executed when the button is pressed?

    You may also supply other subroutines to be called:

    my $button2 = $mw->Button(-text => 'Quit', -command => \&my_sub); sub my_sub { print "Someone clicked me!\n"; }

    There's a large number of Getting Started with Perl/Tk sites out there, and a number of books, too.

    Hope this helped,
    -v
    "Perl. There is no substitute."
Re: Understanding Tk::DialogBox
by castaway (Parson) on May 11, 2005 at 23:07 UTC
    Pressing any of the buttons on a DialogBox causes it to close, the callback you give to -command is called when you press any button, and is given the name of the pressed button as its argument. So, one answer is that you already have a callback sub that gets called.

    The second is that calling Show returns the text of the pressed button, when the DialogBox is closed, so you can also just do:

    my $answer = $config->Show(); myApplySub() if($answer eq 'Apply');

    C.

      Bingo! That's it. Thanks.
Re: Understanding Tk::DialogBox
by scmason (Monk) on May 11, 2005 at 20:01 UTC
    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.