Finding the type of a Tk widget

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值