Version 28 of toplevel

Updated 2004-08-30 16:48:51 by lwv

Documentation for Tk's toplevel command can be found at http://purl.org/tcl/home/man/tcl8.4/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."


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


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?


See also:


Category Widget | Category Command | Tk syntax help | Arts and Crafts of Tcl-Tk Programming