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

Below is the code to a pTk megapwidget I have been working on. It displays the way I want -- Here is the problem: If I hit <RET> everything goes away (desired result). If I press 'Abort' , the pTk frame and its content go away, but I am left with an mwm-frame with a blank grey background (unwanted). Am I asking pTk to do too much? Is there, then, a better way to do this? Since I seem to rewrite this block of code so much , I want to make as much of this into a reuseable megawidget as possible. Its also a reason for the $header_msg which is customiseable.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11141523 use warnings; use Tk; use Tk::DialogBox; use Tk::Frame; { ## OPEN package package Tk::GUIask; use List::Util qw(first); use base qw/Tk::Frame /; Construct Tk::Widget 'GUIask'; ## INSTALL MyNewWidget in pTk namespace sub ClassInit ## Called once to intitalise new class { my ($class , $mw ) = @_; $class ->SUPER::ClassInit($mw); } sub Populate { my ($self , $args ) = @_; my $tv = delete $args->{-textvariable}; # NOTE $self ->SUPER::Populate($args) ; require Tk::Entry; # $self -> SUPER::Populate($args); ## NEEDED? my $search = ""; my $frame = $self -> Frame( -borderwidth => 5, -relief => 'ridge', ) -> pack(); my $label = $frame -> Label ( -fg => 'blue', -font => 30 ) -> pack(-fill => 'x' ); my $entry = $frame -> Entry( -textvariable => $tv, # NOTE ) -> pack(-fill => 'x'); my $button = $frame -> Button ( -background => 'Red', -text => "Abort", -command => sub { ($frame -> destroy +() ) if Tk::Exists($frame); } ) -> pack ( -side => 'top', -fill => ' +x',); $self -> Advertise ('entry' => $frame ); $self -> ConfigSpecs ( DEFAULT => [$frame], text => [$label] ## IF ID DONT have the s +tring 'text => [$label]' ); ## then it will choke on + '-text => $header_msg' ## WHY? $self -> Delegates ( DEFAULT => $frame, bind => $entry, # NOTE ); } ## CLOSE Populate 1; } ## CLOSE package ## Invoke the MEGAWIDGET down here to test ## my $mw = MainWindow -> new; $mw -> geometry('300x300+900+250'); $mw -> title('Main Window '); my $header_msg ="FOOOOO"; my $input = ''; my $dialog = $mw ->DialogBox( -title => ' ', -buttons => []); my $entry = $dialog -> GUIask( -textvariable => \$input, -label => $header_msg )-> pack(); $entry -> bind( '<KeyPress-Return>' , sub # NOTE { print "input = $input\n"; $dialog -> destroy(); } ); $dialog->Show; # NOTE MainLoop;

Replies are listed 'Best First'.
Re: Is this megawidget doable
by tybalt89 (Monsignor) on Mar 02, 2022 at 23:16 UTC

    It's my opinion that you are using DialogBox incorrectly by not having any buttons.

    If you are always putting GUIask in a DialogBox, I would do it this way. If not, please provide a more extensive sample program.

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11141756 use warnings; use Tk; use Tk::DialogBox; use Tk::Frame; { ## OPEN package package Tk::GUIask; use List::Util qw(first); use base qw/Tk::Frame /; Construct Tk::Widget 'GUIask'; ## INSTALL MyNewWidget in pTk namespace sub ClassInit ## Called once to intitalise new class { my ($class , $mw ) = @_; $class ->SUPER::ClassInit($mw); } sub Populate { my ($self , $args ) = @_; $self ->SUPER::Populate($args) ; my $search = ""; my $frame = $self -> Frame( -borderwidth => 5, -relief => 'ridge', ) -> pack(); my $label = $frame -> Label ( -fg => 'blue', -font => 30 ) -> pack(-fill => 'x' ); my $entry = $frame -> Entry( ) -> pack(-fill => 'x'); $self -> Advertise ('entry' => $frame ); $self -> ConfigSpecs ( DEFAULT => [$frame], textvariable => [$entry], # NOTE text => [$label] ); $self -> Delegates ( DEFAULT => $frame, bind => $entry, ); } ## CLOSE Populate 1; } ## CLOSE package ## Invoke the MEGAWIDGET down here to test ## my $mw = MainWindow -> new; $mw -> geometry('300x300+900+250'); $mw -> title('Main Window '); my $header_msg ="FOOOOO"; my $input = ''; my $dialog = $mw ->DialogBox( -title => ' ', -buttons => [qw(OK Abort) +]); my $entry = $dialog -> GUIask( -textvariable => \$input, -text => $header_msg )-> pack(); my $reply = $dialog->Show; # NOTE $reply eq 'OK' and print "input = $input\n"; MainLoop;

      Thanks -- that did what I wanted. I noticed you added the line:

      my $dialog = $mw ->DialogBox( -title => ' ', -buttons => [qw(OK Abort) +]);

      Did this have anything to do with my nutty results? If so , then why?

      OK. So you took the -buttons stuff from the Tk::DialogBox manpage. What if I only want an 'Abort' button that has a red background? Or is that over-complicating the matter?

        If that's what you want, then don't use DialogBox. See Tk::Popup for other choices.