in reply to Tk MainWindow and another TopLevel spawn order

I played around a little bit: by changing forcing secondary_win to generate the phwin (your !Exists block) and by making $mw process one event, I was able to make the main window draw first and end up first in my Win7 application bar...

#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Pane; my $mw = new MainWindow (); $mw->title(" $0 MAIN"); my $phwin; # deleted '= $mw->Toplevel();': force it to be created by s +econdary_win() function my $fr0 = $mw->Frame(-borderwidth => 2, -relief => 'groove' )->pack(); $fr0->Label(-text => "MAIN $0 MAIN" )->pack(); $mw->DoOneEvent(); # appears to force mw to draw itself _before_ seco +ndary_win() creates phwin sleep(1); # makes it obvious that there's a delay; don't use + in production &secondary_win; MainLoop; ###################################################################### +########## sub secondary_win { # window does not exists if (! Exists($phwin)) { $phwin = $mw->Toplevel(); $phwin->title("SECONDARY"); } # window Exists else { $phwin->deiconify( ) if $phwin->state() eq 'iconic'; $phwin->raise( ) if $phwin->state() eq 'withdrawn'; } my $scrolledframe = $phwin->Scrolled('Frame',-scrollbars => 'osoe' +)->pack(); my $sec_label = $scrolledframe->Label(-text=>'SECONDARY')->pack; }

Replies are listed 'Best First'.
Re^2: Tk MainWindow and another TopLevel spawn order
by Discipulus (Canon) on Sep 21, 2016 at 08:05 UTC
    Many thanks pryrt for your investigation!

    DoOneEvent behaves exactly in the way I want and I suspect that was the function Corion suspected to exists. It seems is not so good documented but I found some hints in the a ptkFAQ and a DoOneEvent POD on github.

    I read on Mastering PerlTk:

    It's actually rather difficult to find a use for DoOneEvent. [..] The DoOneEvent statement was an experiment in which we tried various e +vent masks, in an attempt to determine the optimal combination. You s +ee what we arrived at, which, interestingly, is exactly equivalent to +: $mw->update;

    This is true indeed! using $mw->update; has the same effect of your DoOneEvent call.

    Also ++pryrt to spot the mother of all my errors: $phwin = $mw->Toplevel(); must go after the $mw->update; or $mw->DoOneEvent() or just after the statement or in a sub called after (well is the same..).

    I have no precise notions about the Tk loop works and i tend to use idioms that seem to work, of my own or borrowed.

    Obviously master zentara was on our path before us dissecting how much DoOneEvent can be useful; see Re^2: Term::ReadLine and Tk

    Thanks again!

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.