Version 43 of toplevel

Updated 2012-03-31 06:30:10 by Grahack

Documentation for Tk's toplevel command can be found at http://purl.org/tcl/home/man/tcl8.5/TkCmd/toplevel.htm

The toplevel . is created when Tk is initialized.


DKF notes that, "toplevels on UNIX/X are really a collection of several windows; the window you draw on (which is what winfo id will tell you), another window for a menubar (if you've installed one) and a third one to contain the other two. If you do xwininfo -tree you should be able to find out what's really going on."


LV some users of X and the Metacity window manager have reported frustration when new toplevels fail to be raised. A comment on the Debian tcltk-devel mailing list pointing to [L1 ] indicates that if one adds

wm group $w .

after creating toplevel $w, Metacity will raise the windows as expected. Another comment that appeared in the thread indicated that one needed to "... provide a way to send _NET_ACTIVE_WINDOW (and with a correct timestamp!)" when trying to "bring this window to user's attention".


Martin Lemburg: How can I detect if a widget is a toplevel? If I ask a widget for its class, this will fail e.g. at the root widget, because it doesn't return "Toplevel", but "Wish83". And toplevels could be given a class differing from "Toplevel" during their creation. So ... how can I detect a toplevel?


IDG

   [winfo toplevel .]

returns . whereas

   [winfo toplevel .someothertoplevel]

returns .someothertoplevel


MGS - In other words:

   proc istoplevel {W} {
     return [string equal [winfo toplevel $W] $W]
   }

MGS [2003/05/08] - Actually, does a menu count as a toplevel? Probably not (in many cases). So how about this? Toplevels can have a -menu option ...

   proc isToplevel {W} {
     return [expr {[string equal [winfo toplevel $W] $W] &&
                  ![catch {$W cget -menu}]}]
   }

Q. How can I get widget path of all my toplevel windows ?

MGS [2003/08/02] - There is no automatic way, so have to do the work yourself. Try this proc for starters:

 proc toplist {{W .}} {

   set list {}

   if { [string equal [winfo toplevel $W] $W] } {
     lappend list $W
   }

   foreach w [winfo children $W] {
     set list [concat $list [toplist $w]]
   }

   return $list

 }

Then you can use:

  set tplist [toplist]

for a list of all toplevel windows (NOTE: this includes menus as well as strict toplevel windows)

  set tplist [[toplist .toplevel]]

for a list of toplevel windows which are descendants of window .toplevel

MG Aug 31 2004 - That isn't quite true; your [toplist] there doesn't include the window '.' . This should give a complete list of toplevels...

  proc toplist2 {} {

    set list {}
    foreach x ". [winfo children .]" {
             if { $x eq [winfo toplevel $x] && [catch {$x cget -tearoff}] } {
                  lappend list $x
                }
            }
    return $list;
  };# toplist2

MGS Er, yes it does (include window '.').


RS notes that:

   if { [string equal [winfo toplevel $W] $W] } {

can, using expr string comparison, be simplified to

   if {[winfo toplevel $W] eq $W} {

Note: This require Tk 8.4+


MGS [2003/08/24] - Of course there's always wm stackorder, but it only returns mapped windows.


How does one delete a toplevel?

MG Aug 30th 2004 - Just destroy it, with

 destroy $toplevelWindow

Recently on a mailing list someone asked why, when they created a toplevel, that it did not automatically go away when they clicked on the close button/right click and chose close/etc.

Their code was

wm withdraw .
destroy .w
set t [toplevel .w]
wm title $t "main pgm"

The response provided was they would need to also add something like

wm protocol .w WM_DELETE_WINDOW exit

and the following was suggested as an alternative

wm protocol .w WM_DELETE_WINDOW {
 if {[tk_messageBox -parent . -title "Close?" -icon question \
  -type yesno -default no -message "Do You want to close this\
  window"] == yes} {
   exit
 }
}

Note: Any destroy of the main window (aka .) exits the application. There are some other Windows-Events to handle with this, like window size change ...


See also: