在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上)
发布于 2020-02-28 04:03:15
这个错误也出现在Linux上。我为此打开了一个ticket。
解决方法1:
update
focus -force .f.spin3解决方法2:
after 100 [list focus -force .f.spin3]https://stackoverflow.com/questions/60438845
复制相似问题