Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

DialogBox that persists awhile

by DcmbrAgnt (Initiate)
on Jul 06, 2020 at 00:08 UTC ( [id://11118943]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to use a Tk::DialogBox in a way that's probably not the simple case it was meant for:

What I don't like is that when I click the default button, if I have an if statement that runs when you do that, it will go ahead and run that statement, but the dialog disappears while the code is still running. I'd like it to at least stay open long enough for the code in the if statement to finish, so the window can display status on its execution.

I know that DialogBox is ultimately derived from Toplevel, and it's probably just removing its window as soon as any of its bottom buttons are clicked because that's what it's written to do.

I've read that Tk::Derived is supposed to support making a derivative copy of a widget so you can then customize, but I've never done that before. Could it all be done inside the same Perl script as the program, or would some sort of external module be needed? What would be needed to make a version of DialogBox that lets you have more control over when the window goes away?

Replies are listed 'Best First'.
Re: DialogBox that persists awhile
by tybalt89 (Monsignor) on Jul 06, 2020 at 20:29 UTC

    You didn't provide a Short, Self-Contained, Correct Example so I tossed a bunch of guesses into a pot and stirred...

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11118943 use warnings; use Tk; use Tk::DialogBox; use Time::HiRes qw( sleep ); { # start package package Tk::AltDialogBox; use base qw( Tk::DialogBox ); Tk::Widget->Construct('AltDialogBox'); sub Wait { my $cw = shift; $cw->Callback(-showcommand => $cw); $cw->waitVariable(\$cw->{'selected_button'}); $cw->grabRelease if Tk::Exists($cw); $cw->Callback(-command => $cw->{'selected_button'}); # swapped with +next line $cw->withdraw if Tk::Exists($cw); } } # end package my $message = ' '; my $mw = MainWindow->new; $mw->geometry( '200x200+1000+300' ); my $lb; my $d; $d = $mw->AltDialogBox( -buttons => ['OK', 'Cancel'], -command => sub { my $value = $lb->get($lb->curselection // 0); $message = "working on $_ ...\n", $d->update, sleep 0.3 for 1 .. 1 +0; print "exited by @_ with $value\n"; }, -showcommand => sub { $lb->selectionClear(0, 'end'); $lb->selectionSet(0); $message = ' '; }, ); $lb = $d->add(Listbox => -height => 10)->pack; $d->add(Label => -textvariable => \$message, -height => 2)->pack; $lb->insert('end' => 1 .. 5); $mw->Button(-text => 'See Dialog', -command => sub { $d->Show }, )->pack; MainLoop;
Re: DialogBox that persists awhile
by kcott (Archbishop) on Jul 06, 2020 at 06:36 UTC

    G'day DcmbrAgnt,

    "... display status on its execution."

    I'm having some problems envisaging what you want to do here. If it's a simple action, then some form or status bar might be appropriate. Perhaps it's a long-running action and you want a progress bar. If there are multiple messages that you want to keep, maybe report those in their own widget.

    "Could it all be done inside the same Perl script as the program, ..."

    You can use Tk::Toplevel in your Tk script. You can customise this however you want.

    "I've read that Tk::Derived ... but I've never done that before."

    I wrote some information on that just a few weeks ago: "Re: Writing my first PERL/Tk megawidgit".

    — Ken

Re: DialogBox that persists awhile
by Anonymous Monk on Jul 06, 2020 at 07:33 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11118943]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-03-28 17:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found