ttk::button 保持宽度 [TCL/TK]

ttk::button keep width [TCL/TK]

当我点击 .tbutton4 时,我希望当我通过 "Unlock" 更改文本时 .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

感谢您的帮助

可以通过设置按钮小部件的 -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), grid, if, ttk::button (widget), ttk::frame (widget)