How to implement a button whose functionality is a cross between radiobutton and checkbutton?

非 Y 不嫁゛ 提交于 2019-12-04 18:47:15

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

(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 sounds like you just need to listen for when a checkbook is checked, and disable / enable the other boxes based on that. You might also need to keep a "disabled" count for each box, and make sure that each box is only enabled when the disable count is zero. Therefore checking C3, then C1, then C1 again, will not re-enable C4, as C4 will still have a disable count of one. Checking C3 again will in fact make the disable count of C4 zero, and C4 should be re-enabled.

You could generalize this functionality, and link the checkboxes in some descriptive manner, rather than a functional manner.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!