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

Hello,

I'm writing a Perl/Tk script (my first GUI ever) and I have two problems that look to me like steming from the same cause (which might be a very basic misunderstanding of how things should work in GUI).

The program sometimes opens a warning popup which I initially implemented as a "Dialog" widget. It worked as it should at first - clicking the "OK" button closed the dialog and the main program continued doing what it warned me about. But when I added an additional "Toplevel" to the program the troubles began: if the warning popup appeared while the additional "Toplevel" is opened, clicking on te "OK" button doesn't cause the popup window to close and in fact, the whole program stops responding to buttons clicks, scroll sliders and the only thing that works is clicking on main window/additional "Toplevel"/warning popup brings it to front.

After googling on multiple "Toplevel"-s and "Dialog" and finding nothing that helped, I dove into the code of the packages (Dialog.pm, DialogBox.pm) and found in DialogBox.pm the function:

sub Wait { my $cw = shift; $cw->Callback(-showcommand => $cw); $cw->waitVariable(\$cw->{'selected_button'}); $cw->grabRelease if Tk::Exists($cw); $cw->withdraw if Tk::Exists($cw); $cw->Callback(-command => $cw->{'selected_button'}); }

I placed some debug prints (sorry for ugly way of debugging) in various places and found that the code get stuck on:

$cw->waitVariable(\$cw->{'selected_button'});

I also placed debug prints on the value of

$cw->{'selected_button'}

itself and saw it changes from "" to "OK" when I click the "OK" button. It seems I don't understand how the "waitVariable" works: it looks to me the monitored variable changes regardless of weather the additional "Toplevel" is opened or not but the "Dialog" code seems to "miss" the click if additional "Toplevel" is opened or to respond to click if no additional "Toplevel"is opened.Here's the "Dialog" creation code:

my $gui_mw_wrapper = new MainWindow; my $gui_warn_text; . . . $gui_warn_text = $gui_mw_wrapper -> Dialog( -title=>$title, + -text=>$message, + -width=>$text_width, + -font=>"Times 12 normal", + -buttons=>["OK"], + -wraplength=>$text_width_pixels); $dbg_answer = $gui_warn_text -> Show();

and the code creating the additional "Topleve":

my $gui_src_wrapper_frm_wr; . . . $gui_src_wrapper_frm_wr = $gui_mw_wrapper -> Toplevel( + -title=>"Tree", + -bo +rderwidth=>1);

In frustration I changed the warning popup to a "Toplevel" where I placed a "ROText" and the "OK" button which destroys this "Topleve" and the exit of the function is blocked by "waitWindow" until it's get destroyed by the button. In this version clicking the "OK" button closed the popup window (always) but the code couldn't pass the "waitWindow" if additional "Toplevel" was opened when the warning popup was activated. Wihtout the additional "Toplevel" opened it worked OK. Here's the code from the warning popup of the second kind:

$gui_warn_frm = $gui_mw_wrapper -> Toplevel( -title=>$title +, + -relief=>"ridge", + -borderwidth=>3); $gui_warn_text = $gui_warn_frm -> Scrolled( "ROText", + -scrollbars=>"ose", + -font=>"Times 12 normal", + -width=>$text_width, + -height=>$text_height, + -wrap=>"word"); $gui_warn_button = $gui_warn_frm -> Button( -text=>"OK", + -command=>[sub {my($ptr) = @_; $ptr -> dest +roy();},$gui_warn_frm]); $gui_warn_frm -> geometry("+300+50"); $gui_warn_frm -> raise($gui_mw_wrapper); $gui_warn_text -> grid( -row=>0, -column=>0); $gui_warn_button -> grid( -row=>1, -colum +n=>0); $gui_warn_text -> insert("end",$message); $gui_mw_wrapper -> update; $gui_warn_frm -> waitWindow;

What am I missing here? I attempted to google things like "waitVariable multiple Toplevel" or "waitVariable implementation" or similar things but found nothing that helped me.

TIA for any help, K.A.

Replies are listed 'Best First'.
Re: waitVariable/waitWindow don't work when additional Toplevel opened
by Anonymous Monk on Nov 08, 2011 at 10:21 UTC
Re: waitVariable/waitWindow don't work when additional Toplevel opened
by zentara (Cardinal) on Nov 08, 2011 at 11:08 UTC
    Yeah, show a complete minimal example of your problem. It seems you don't understand how global grabs work. If we knew exactly what you were trying to do, we could offer solutions.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh