如何实现一个功能介于单选按钮和复选按钮之间的按钮?

发布于 2024-11-26 07:06:59 字数 651 浏览 1 评论 0原文

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

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应该变灰。

如果有一种优雅的方法来实现 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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2024-12-03 07:06:59

到目前为止,实现复杂的启用和禁用模式的最简单机制是在触发器变量(c1c3)上放置一个trace例如),这样每当它们改变时,你就重新计算状态。

# _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 带有用于大多数基本功能的内置控制器工作 - 但一切都是直接工作的,您可以设置一次,之后就不再摆弄它。)

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

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.

Smile简单爱 2024-12-03 07:06:59

听起来您只需要监听支票簿何时被选中,并根据该情况禁用/启用其他框。您可能还需要为每个框保留“禁用”计数,并确保每个框仅在禁用计数为零时才启用。因此,检查 C3,然后检查 C1,然后再次检查 C1,不会重新启用 C4,因为 C4 的禁用计数仍为 1。再次检查C3实际上会使C4的禁用计数为零,并且应该重新启用C4。

您可以概括此功能,并以某种描述性方式(而不是功能性方式)链接复选框。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文