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

Hi All, I have a Tk Dialog Box that looks something like this:
my $Dialog = $mw->DialogBox( -title=>"Add Comments", -buttons=>["OK","Cancel"] ); $Dialog->add('Label', -text=>"Comments:" )->pack(-side=>"top",-anchor=>"w"); $Dialog->add('Text', -width=>30, -height=>10 )->pack(-side=>"top",-anchor=>"w");
My problem is that if someone wants to enter in their comments and they hit 'enter' while typing in the text box, the dialog box captures that as an event and automatically pushes its default button, which closes the dialog. This is quit annoying, is there anyway to unbind "<Return>" from the dialog box? The documentation says "DialogBox" only supports a "Show" and an "add" method.

Replies are listed 'Best First'.
Re: Tk DialogBox Question
by bbfu (Curate) on Aug 29, 2003 at 17:28 UTC

    #!/usr/bin/winperl use warnings; use strict; use Tk; use Tk::DialogBox; my $dlg = MainWindow->new()->DialogBox( -title => 'Test', -buttons => [qw/Ok Cancel/], ); $dlg->Entry()->pack->focus(); # Destroy the current binding for <Return> $dlg->bind('<Return>', ''); print "Button pressed: ", $dlg->Show(), "\n";

    Update: Further explaination: A Tk::DialogBox is just a widget (a composite widget, but still just a widget), so you can call any standard widget methods on it, such as configure or bind. The docs for bind say that if you give an empty string for your callback, it destroys the current binding for that sequence.

    bbfu
    Black flowers blossom
    Fearless on my breath

      Thanks, I just did the $dlg->bind('<Return>', ''); and that changed the behavior exactly as desired. ++ for you
      That explains some confusion I had. Thanks ++
Re: Tk DialogBox Question
by jdtoronto (Prior) on Aug 29, 2003 at 17:25 UTC
    IIRC it is not possible, Tk::Dialog and Tk::DialogBox only want a simple string. You may need to build your own window if you want to allow for return in the returned string. Tk::DialogBox always sets the first element of the array passed in -buttons as the default if none is specified.

    Alternatively, Tk::Dialog does not mention a default setting if one is not specified, maybe it only binds return if specifically asked to?

    Try it :)

    jdtoronto