Updated 2015-01-06 00:56:57 by aspect

(snappy description of a toplevel belongs here)
toplevel pathname ?option ..
http://www.tcl.tk/man/tcl/TkCmd/toplevel.htm

The toplevel . is created when Tk is initialized.

See Also  edit

Maximizing a toplevel window
winfo toplevel
binding to a toplevel window
A fading Window
   [Introduction to Toplevels] (link is broken, check [archive.org])

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."

Controlling toplevel raise behaviour edit

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 [1] 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".

Detecting if a widget is a toplevel edit

winfo toplevel will tell you the toplevel associated with a given widget. Unfortunately, menus are toplevels too - they can be distinguished in the catch below:
proc is_toplevel {w} {
    expr {[winfo toplevel $w] eq $w && ![catch {$w cget -menu}]}
}

Enumerating toplevel windows in an application edit

MGS [2003/08/02] - There is no automatic way, so have to do the work yourself. This proc uses winfo children to traverse the widget hierarchy.
proc get_toplevels {{w .}} {
    set list {}
    if {[is_toplevel $w]} {
        lappend list $w
    }
    foreach w [winfo children $w] {
        lappend list {*}[get_toplevels $w]
    }
    return $list
}

In a more functional programming style (with some helper procs), this might be written:
    lfilter {not is_toplevel} [transitive-closure {winfo children} $w]

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

 }

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

Deleting toplevels edit

MG Aug 30th 2004 - Just destroy it, with
 destroy $toplevelWindow

alternatively, as any other Tcl command:
 rename $toplevelWindow .

Making your program exit when its main window is closed edit


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 ...

Uncategorised tips, tricks & questions edit

[Peter Hiscocks] I was having trouble getting using a topview as an alert. The topview would appear but without content. I discovered that an update command at the end of the alert procedure worked to fill in the toplevel.