Version 37 of toplevel

Updated 2008-01-21 11:53:34 by dkf

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 (which winfo frame will tell you). If you do xwininfo -tree you should be able to find out what's really going on."


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 toplist [toplist]

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

  set toplist [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

See also: