Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

How to bring a Tk window to the top

by rinceWind (Monsignor)
on May 06, 2003 at 14:55 UTC ( [id://255903] : perlquestion . print w/replies, xml ) Need Help??

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

I read with interest the thread "Always on top" with Win32 and Tk. However, what I want to do is different - I don't want a top level window on top all of the time. From a callback, I want to make a pre-existing window to pop up and bring itself to the front.

I think the answer lies in the toplevel window taking focus, but I can't find this in the documentation.

The platform is X-window on Solaris.

Any clues? Thanks in advance.

Replies are listed 'Best First'.
Re: How to bring a Tk window to the top
by converter (Priest) on May 06, 2003 at 16:53 UTC

    If the toplevel hasn't been mapped yet or has been withdrawn you'll want to invoke the deiconfiy method to make sure the window is mapped in de-iconified form, and then the raise method to bring the window to the top of the stacking order. The Tk::Widget and Tk::Wm man pages should be helpful.

    This quickie demo should give you a good start:

    use Tk; $mw = tkinit; $t = $mw->Toplevel; $t->withdraw; $t->Label(-text => "Testing...")->pack; $t->Button( -text => "Withdraw", -command => sub {$t->withdraw}, )->pack; $mw->Button( -text => 'Test', -command => sub { $_->deiconify, $_->raise for $t; }, )->pack; MainLoop;
Re: How to bring a Tk window to the top
by perlplexer (Hermit) on May 06, 2003 at 15:31 UTC
Re: How to bring a Tk window to the top
by LD2 (Curate) on May 06, 2003 at 15:29 UTC
    this documentation may help a bit...
Re: How to bring a Tk window to the top
by zakb (Pilgrim) on May 06, 2003 at 15:33 UTC

    This is a bit of a guess, but the Tk::Wm POD mentions a focus method.

    Maybe $toplevel->focus will work?

Re: How to bring a Tk window to the top
by Necos (Friar) on May 06, 2003 at 23:04 UTC
    How come no one has mentioned the focusForce() method? The raise() method will bring a window to the foreground, but another window may have focus. focusForce will force a window to take the focus. Of course, this is not always nice, but that is what rinceWind wants. (:

    Hope that helps some,

    Theodore Charles III
    Network Administrator
    Los Angeles Senior High
    perl -e "map{print++$_}split//,Mdbnr;"

      From the Tk::focus POD:

      $widget->focusForce Sets the focus of $widget's display to $widget, even if the + appli- cation doesn't currently have the input focus for the displ +ay. This command should be used sparingly, if at all. In norma +l usage, an application should not claim the focus for itself; inst +ead, it should wait for the window manager to give it the focus.

      I've found only one case where focusForce was required to get focus back to one of my windows, and that was after a call to getOpenFile from a composite widget. I'm still not sure if focusForce was required because of something I was doing wrong or a focus bug introduced by getOpenFile (I suspect the former).

      If raising a window doesn't cause the window manager to give it focus, then $widget->focus should be sufficient.