在TCL中,我发现在第一次focus
调用后无法更改焦点
这是一个MRE:
frame .f -padx 15 -pady 15
spinbox .f.spin1 -from 0 -to 10 -width 5 -relief solid -highlightthickness 1 -highlightcolor blue -borderwidth 1 -textvariable ::a
spinbox .f.spin2 -from 0 -to 10 -width 5 -relief solid -highlightthickness 1 -highlightcolor blue -borderwidth 1 -textvariable ::b
spinbox .f.spin3 -from 0 -to 10 -width 5 -relief solid -highlightthickness 1 -highlightcolor blue -borderwidth 1 -textvariable ::c
# note that changing the focus in the bindings works as expected
bind .f.spin1 <Key> { if {"%K" eq "Left"} { focus .f.spin3 } }
bind .f.spin1 <Key> {+ if {"%K" eq "Right"} { focus .f.spin2 } }
bind .f.spin2 <Key> { if {"%K" eq "Left"} { focus .f.spin1 } }
bind .f.spin2 <Key> {+ if {"%K" eq "Right"} { focus .f.spin3 } }
bind .f.spin3 <Key> { if {"%K" eq "Left"} { focus .f.spin2 } }
bind .f.spin3 <Key> {+ if {"%K" eq "Right"} { focus .f.spin1 } }
pack .f
grid .f.spin1 -column 1 -row 0
grid .f.spin2 -column 2 -row 0
grid .f.spin3 -column 3 -row 0
# with no focus invocation, no spinbox will be focused when the window opens
# we'll call focus on spin2 as default
# this works as expected
focus -force .f.spin2
# if some condition is met we want to change the focus to spin3
# but the following invocation doesn't change the focus
if { 1 } {
focus -force .f.spin3
}
我也尝试过(没有成功(专注于spin3 之前的帧
focus -force .f.spin2
focus -force .f
focus -force .f.spin3
我确实尝试过(再次没有成功(使用after idle
after idle { focus -force .f.spin3 }
确实使用了after 1000
,但它在代码方面并不优雅。而且对用户也不好,因为他可能会在焦点改变之前键入。我尝试过较小的值,但焦点不会因为较小的值而改变,所以这不是一个可靠的解决方案。
为什么会发生这种情况?我该怎么修?
编辑:(不确定是否相关,但我在Win10上(
这个错误也出现在Linux上。我为此开了一张票。
解决方法1:
update
focus -force .f.spin3
解决方法2:
after 100 [list focus -force .f.spin3]