如何实现一个功能介于单选按钮和复选按钮之间的按钮? [英] How to implement a button whose functionality is a cross between radiobutton and checkbutton?

查看:17
本文介绍了如何实现一个功能介于单选按钮和复选按钮之间的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据用户选择的选项动态禁用或启用 checkbutton.我想要的是 radiobuttoncheckbutton 之间的交叉.

I want to dynamically disable or enable checkbuttons based on the options the user selects. What I would like is a cross between radiobutton and checkbutton.

checkbutton .c1 -text "C1" -variable c1
checkbutton .c2 -text "C2" -variable c2
checkbutton .c3 -text "C3" -variable c3
checkbutton .c4 -text "C4" -variable c4

grid .c1 -sticky w
grid .c2 -sticky w
grid .c3 -sticky w
grid .c4 -sticky w

在上面的例子中,如果我选中C1,那么C2C4选项应该是灰色的.同样,如果我检查 C3 那么 C4 应该是灰色的.

In the above example, if I check C1, then the options C2 and C4 should be grayed out. Similarly, if I check C3 then C4 should be grayed out.

如果有一种优雅的方法可以实现 radiobuttoncheckbutton 之间的这种交叉,并且随着选项数量的增加也可以很好地扩展?

It would be great if there is an elegant method which implements this cross between radiobutton and checkbutton which also scales well as the number of options increases?

推荐答案

到目前为止,实现这种复杂的启用和禁用模式的最简单机制是在触发器变量(trace>c1 和 c3 在您的示例中),以便每当它们更改时,您都会重新计算状态.

By far the simplest mechanism for implementing that complex pattern of enabling and disabling is to put a trace on the trigger variables (c1 and c3 in your example) so that whenever they change you recompute the states.

# _After_ initializing the state...
trace add variable c1 write reconfigureButtons
trace add variable c3 write reconfigureButtons
proc reconfigureButtons args {
    global c1 c3
    .c2 configure -state [expr {$c1      ? "disabled" : "normal"}]
    .c4 configure -state [expr {$c1||$c3 ? "disabled" : "normal"}]
}

(从概念上讲,您将控制器的一部分挂在模型上而不是视图上,因为这更标准"——我们通常不谈论 Tk 的 MVC,因为 Tk 带有内置的控制器对于最基本的工作——但一切都可以直接进行,你可以设置一次,之后就不用再摆弄它了.)

(Conceptually, you're hanging a piece of the Controller off the Model instead of off the View as is more "standard" — we don't normally talk in terms of MVC for Tk because Tk comes with built-in Controllers for most basic work — but it all works straight-forwardly and you can set it up once and not fiddle around with it afterwards.)

但是,不要混淆检查按钮和单选按钮(正如您的问题文本似乎表明的那样).请.这是因为它们为用户提供了不同的视觉提示:复选按钮是开关,单选按钮是选择其中一个",并且以任何其他方式使用它们只会让事情变得更难使用而没有其他好处.

However, don't mix up checkbuttons and radiobuttons (as your question text seems to indicate). Please. That's because they offer different visual cues to users: checkbuttons are on-off switches, radiobuttons are "pick one of these", and using them any other way will just make things harder to use for no other benefit.

这篇关于如何实现一个功能介于单选按钮和复选按钮之间的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆