ttk ::按钮保持宽度[TCL / TK]

时间:2016-10-25 19:46:42

标签: tcl tk

当我点击我的.tbutton4时,我希望当我通过“解锁”更改文本时.tbutton3不会移动。在我的代码下面:

ttk::frame .toolbar

ttk::button .tbutton3 -text "Exit" -style "Toolbutton" -command {}
ttk::button .tbutton4 -text "Lock" -style "Toolbutton" -command {
if {[.tbutton4 cget -text] eq "Lock"} {
.tbutton4 config -text "Unlock"
} else {
.tbutton4 config -text "Lock"
}
}

grid .toolbar -row 0 -column 0 -sticky news

grid .tbutton4 -in .toolbar -row 0 -column 0 -padx 2 -pady 2 -sticky w
grid .tbutton3 -in .toolbar -row 0 -column 1 -padx 2 -pady 2 -sticky w

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

可以通过设置按钮小部件的-width选项来修复宽度。

ttk::frame .toolbar

ttk::button .tbutton3 -text "Exit" -style "Toolbutton" -command {}
ttk::button .tbutton4 -text "Lock" -style "Toolbutton" -width 7 -command {
    if {[.tbutton4 cget -text] eq "Lock"} {
        .tbutton4 config -text "Unlock"
    } else {
        .tbutton4 config -text "Lock"
    }
}

grid .toolbar -sticky news

grid .tbutton4 .tbutton3 -in .toolbar -padx 2 -pady 2 -sticky w

这对我看起来相当不错,根据主题等可能需要更多调整。使用宽度的幻数有点不确定:一旦确定了最佳宽度,就可能将其与字符串长度相关联最长的文字;例如expr {[string length Unlock] + 1}

文档: eq (operator)gridifttk::button (widget)ttk::frame (widget)