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

Hi Monks,
I have an issue with the dialog box working
my $db = $mw->DialogBox(-title => 'Dialog', -buttons => ['Yes','No','A +bort'],-default_button => 'Yes'); $db->Label(-text =>"This is a dialog")->pack(-side=>'top', -ipady=>5); my $answer = $db->Show(); print "Your selection is $answer \n";
The above code works normally when we select the buttons Yes, No, Abort using the mouse or the space bar but when we select it using the enter key <Return> the default button value in my case i.e 'Yes' is seleted inspite what ever the value that is selected.
please let me know how to overcome this issue ??

Replies are listed 'Best First'.
Re: Dialog box Binding issue
by BrowserUk (Patriarch) on Dec 12, 2008 at 09:59 UTC

    I think your expectations are wrong. (If your running on windows; ignore this post otherwise.)

    The <enter> key will always select the default value, regardless of whether the user has moved the input focus prior to hitting the enter key.

    To select a non-default value, you must move a) the input focus; b) select that option using the <spacebar>. This is WAD.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Dialog box Binding issue
by GrandFather (Saint) on Dec 12, 2008 at 09:59 UTC

    The documentation says "by typing return to invoke the default Button (if any)". The behavior you see is by design and conventional.


    Perl's payment curve coincides with its learning curve.
      Hi, my issue is when i want to select 'No' Button, i press a Tab button and now the focus will be on No and press Return Key even in that case "YES" is selected :(

        That is your issue maybe, it is not a bug in the DialogBox code. It is expected behaviour and it is by design. That is the way all dialogues work in Windows. To activate the control that has focus use the space bar, not the enter key - the enter key always "presses" the default button.


        Perl's payment curve coincides with its learning curve.
        This is a bug in Tk::DialogBox, you should report it here. To test run widget, under "Common Dialogs" click on "1. Message boxes.", when "messageBox Demo" pops up, choose Type OkCancel, then click "Message Box" button, and perform the test (tab over to cancel button, hit enter). Default is "Tk::DialogBox", and selecting Cancel and hitting enter doesn't work (falsely returns that you selected Ok). If you click radio button to switch to Tk::MsgBox , and perform test again, you will get Cancel as expected.
Re: Dialog box Binding issue
by imrags (Monk) on Dec 12, 2008 at 09:30 UTC
    Why don't you use RadioButton or PopUP or Tk::Widget instead?
    Raghu
Re: Dialog box Binding issue
by zentara (Cardinal) on Dec 12, 2008 at 18:03 UTC
    If you are willing to use Gtk2, you can easily make a dialog work the way you want. This will let you tab around to the buttons, and enter will then select the right button.
    #!/usr/bin/perl use warnings; use strict; use Glib qw(TRUE FALSE); use Gtk2 '-init'; my $dialog = Gtk2::Dialog->new ('Confirmation Dialog', undef, 'modal', 'gtk-ok' => 'ok', # response ids may be one of the built-in + enums... '_Cancel' => 2, # or any positive integer. 'gtk-undo' => 'reject', 'gtk-save' => 'accept', 'gtk-cancel' => 'cancel', 'Do it' => 'ok', ); # put an hbox with a label and entry into the dialog's vbox area. my $hbox = Gtk2::HBox->new (FALSE, 6); $hbox->pack_start (Gtk2::Label->new ('Continue Real Transfer? '), TRUE +, TRUE, 0); $hbox->show_all; $dialog->vbox->pack_start ($hbox, TRUE, TRUE, 0); # Set which response is the default: $dialog->set_default_response ('ok'); # connect a signal to the dialog's response signal. $dialog->signal_connect (response => sub { print "@_\n"; # get our params shift; # we don't need the dialog my $response = shift; # the clicked button print "The user clicked: $response\n"; if ($response eq 'ok'){ Gtk2->main_quit; } if ($response eq 'cancel'){ return } #more if's and elsif's...... }); # show the dialog $dialog->show; Gtk2->main;

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Dialog box Binding issue
by zentara (Cardinal) on Dec 12, 2008 at 15:27 UTC
    You could override the binding yourself, for example
    $dialogBox->bind('<Return>' => undef);
    that would force the user to click a button.

    Additionally, you can play with the binding when shifting focus on tabs, like

    $dialogBox->bind('<Tab>' => sub{ #get currently focused button $dialogBox->configure(-focus => $my_focussed_button); # there is also forceFocus which may help force it });
    As a last resort, you can always make your own dialog out of a toplevel window, and setup the bindings to behave anyways you want.

    I'm not really a human, but I play one on earth Remember How Lucky You Are