如何实现一个按钮,其功能是单选按钮和检查按钮之间的交叉? [英] How to implement a button whose functionality is a cross between radiobutton and checkbutton?

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

问题描述

我想基于用户选择的选项动态禁用或启用检查按钮。我想要的是 radiobutton checkbutton 之间的交叉。

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 ,那么选项 C2 C4 应该是灰色的。
同样,如果我检查 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.

如果有一个优雅的方法在 radiobutton

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?

推荐答案

到目前为止,最简单的机制用于实现启用和禁用的复杂模式是在触发器变量( c1 和<$ c)上放置跟踪

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"}]
}

一个控制器关闭模型,而不是关闭视图作为更多的标准 - 我们通常不谈论MVC的Tk,因为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.)

但是,不要混淆checkbuttons和radiobuttons(因为你的问题文本似乎表明)。请。这是因为他们为用户提供了不同的视觉提示:检查按钮是开关,单选按钮选择其中一个,使用任何其他方式将只是使事情难以使用没有其他好处。

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天全站免登陆