如果按下按钮还设置了焦点,如何禁止按钮命令?

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

我的应用程序有一个绑定到鼠标按钮的命令。当你点击一个窗口来设置焦点时,我已经意识到它很烦人,它也会执行按钮命令。

以下是将命令绑定到按钮1的行:

bind .f.canvas <Button-1> {panto %W %x %y 0.5}; # pan half distance

如何在按下按钮时禁止按钮命令的调用来设置焦点?


这是我的“测试序列”:

  1. 开放计划
  2. 单击画布。所需/结果:平移到点击位置。
  3. 单击桌面。
  4. 单击画布。期望:没有锅。结果:平移到单击位置。
  5. 单击画布。所需/结果:平移到点击位置。
tcl tk
2个回答
0
投票

如果使用break完成绑定脚本,它将不会运行与该事件关联的任何其他脚本(例如,在窗口小部件级别,整个窗口级别或整个应用程序级别)。具体来说,你这样做:

bind .f.canvas <Button-1> {
    panto %W %x %y 0.5; # pan half distance
    break
}

这种事情是建议使用多命令绑定脚本的少数情况之一。通常,将事物提取到过程中会更好(为了易于理解),但在这种情况下,最好直接放入break


0
投票

我目前的解决方案有点像黑客。我骑在条目小部件上的键盘焦点事件上骑背,在条目不在焦点时暂时禁用按钮命令:

ttk::entry .f.cmd -textvar e -width 30
bind .f.cmd <FocusOut> {
  puts "focus out, disable button press"; 
  bind .f.canvas <Button-1> {}
}
bind .f.cmd <FocusIn> {
  puts "focus in, enable button press starting next event"; 
  after idle {
    bind .f.canvas <Button-1> {
      panto %%W %%x %%y 0.5
    }
  };
}

在Win10上测试,Tcl 8.6。

© www.soinside.com 2019 - 2024. All rights reserved.