The winfo class
command will tell you the class of a widget, which is usually good enough (see this interactive session):
% label .tklabel; puts [winfo class .tklabel] Label % ttk::label .ttklabel; puts [winfo class .ttklabel] TLabel
Note that you can set the class of standard Tk toplevels and Ttk widgets at creation time. This can make things rather complex. With toplevels, you can see whether [winfo toplevel $w] eq $w
is true, but that's not a guaranteed test from 8.5 onwards, as it is possible to reparent toplevels as children of another widget or turn classic frames into toplevels (via wm forget
/wm manage
).
If you have to get the exact command used to create a widget, the only truly sure method is to trap the creation command and log the info, like this:
rename frame the_real_frame proc frame {pathName args} { set ::creationInfo($pathName) [list frame $args] the_real_frame $pathName {*}$args }
It's usually easier to try to avoid such complexity (especially as in a production setting you'll also need to set things up to deal with <Destroy>
events so that you clean up information about no-longer-extant widgets, and that just adds lots more trickiness).