Tcl / Tk:是否可以使用自省或反射的方法来获取Tcl / Tk小部件的选项和命令列表?

问题描述 投票:2回答:2

Tcl / Tk:是否有一种方法可以使用自省或从Tcl复制来获取Tcl / Tk小部件的选项和命令列表?

[我在想类似Python的东西:X.__dict__,它返回任意对象x的属性的字典。

我尝试过infowinfo,但似乎并没有解决问题

如果可以通过代码访问这些属性,那将节省我手工编写,逐个窗口小部件进行编码的工作量(我正在尝试创建“还另一个Tcl / Tk”绑定...)

提前感谢!

reflection tcl tk introspection
2个回答
2
投票

要获取窗口小部件的选项列表,请使用configure命令而不使用其他选项。

% button .b
.b
% puts [join [lmap c [.b configure ] {if {[llength $c] == 2} continue; lindex $c 0}] \n]
-activebackground
-activeforeground
-anchor
-background
-bitmap
-borderwidth
-command
-compound
-cursor
-default
-disabledforeground
-font
-foreground
-height
-highlightbackground
-highlightcolor
-highlightthickness
-image
-justify
-overrelief
-padx
-pady
-relief
-repeatdelay
-repeatinterval
-state
-takefocus
-text
-textvariable
-underline
-width
-wraplength

如果您还没有安装8.6,则必须这样做:

foreach config [.b configure] {
    if {[llength $config] == 2} continue
    puts [lindex $config 0]
}
# Same output

获得子命令列表的最简单方法(但是您可以根据需要调用它们的方法)是查看错误消息。

% .b ?
bad option "?": must be cget, configure, flash, or invoke

按钮没有那么多子命令。

可以catch和一些正则表达式编写脚本,消息的格式非常时尚,但是除了交互之外,没有什么其他意义了;代码不会知道when使用方法(也不会对how使用它有更好的了解)。教过它后,您就不需要通用的内省者了…


所有小部件都将具有configurecget。如果不是,则它不是小部件。其他一切都取决于班级。您可以使用winfo class来实现,但是某些小部件可以在创建时进行更改。当然,除了阅读specific问题之外,没有其他方法可以阅读文档。


0
投票
proc saveOptions {} {
    set f [open saved_defaults w+]
    foreach w {button checkbutton \
         radiobutton menubutton \
         entry menu label spinbox \
         listbox canvas scrollbar scale frame} {
        set x [$w .xx]        
        foreach b [$x configure] {
            if {[llength $b] == 2} continue;
            puts $f "*[string totitle $w].[lindex $b 1]:[lindex $b end]"
        }
        destroy $x
      }
      close $f
  }
© www.soinside.com 2019 - 2024. All rights reserved.