【问题标题】:"Previous-Next" Button instead of Slider in Mathematica?Mathematica 中的“上一个-下一个”按钮而不是滑块?
【发布时间】:2015-08-06 19:13:14
【问题描述】:

当控制值是离散的时,是否可以使用一些“上一个-下一个”按钮而不是滑块,如下例所示?

我发现 Manipulator 很丑,如果可能的话,我想要一些 Setter 类型的。

Manipulate[
           Graphics[
                    {
                     Rectangle[{1, 1}, {3, 3}],
                      Circle[{where, 2}, 1]
                    }, 
                     PlotRange -> {{0, 11}, {0, 3}}, ImageSize -> {300, 60}
                    ],
           {where, 1, 10, 1, Slider}
          ]

【问题讨论】:

    标签: slider controls wolfram-mathematica


    【解决方案1】:

    您可以像这样使用Button 创建自己的控件:

    Manipulate[
     Graphics[
      {Rectangle[{1, 1}, {3, 3}],
       Circle[{where, 2}, 1]},
      PlotRange -> {{0, 11}, {0, 3}},
      ImageSize -> {300, 60}
      ],
     {{where, 1, ""}, 
      Button["Prev", where = Max[1, where - 1], Appearance -> "Palette", 
        ImageSize -> {50, Automatic}] &},
     {{where, 1, ""}, 
      Button["Next", where = Min[10, where + 1], Appearance -> "Palette",
        ImageSize -> {50, Automatic}] &},
     ControlPlacement -> Left]
    

    【讨论】:

    • @500,如果它解决了你的问题,你应该接受这个答案。
    • @Joshua,我允许自己让它无人回答,让其他专家有时间提出他们的解决方案。从我发布的问题和其他问题中,我观察到答案不是多余的,而是适合不同的约束。因此,当我觉得这个问题引起了人们的兴趣时,我将其置之不理。我给自己一两天时间。尤达答案是我使用的答案,它将被选中。 “只有在语法方面,你才能做到完美。” (William Safire) 这最好地概括了我的想法和我对这个论坛的尊重。不过,在这方面,我很乐意调整自己的行为。
    • @Joshua 我发现相反的情况(即,接受似乎在发布后几秒钟就可以解决问题的第一个答案)更烦人。如果这是我们的默认行为,我们将不得不像秃鹰一样一直绕圈子,而我还有更多的事情要做。
    • @Joshua:如果已经有一个公认的答案,很多人都不会费心回答,如果一个人草率,可能会错过有经验的人提供更好的答案或补充现有答案的答案.例如,在这个问题中,Sjoerd 的回答很好地补充了我的答案,并解释了如何在端点禁用按钮。这是我不知道该怎么做的事情,它不仅对 OP 有帮助,而且对我也可能对其他人也有帮助。
    【解决方案2】:

    据我所知,如果没有为 Manipulate 指定控件,那么 Mathematica 将根据给定的值决定使用什么控件。这通常是 Manipulator 控件,它不同于 Slider 控件(在您的示例中),因为可以扩展控件以使用前进/后退、播放功能等。这些可能就足够了:

    Manipulate[
     Graphics[{Rectangle[{1, 1}, {3, 3}], Circle[{where, 2}, 1]}, 
      PlotRange -> {{0, 11}, {0, 3}}, ImageSize -> {300, 60}], {where, 1, 
      10, 1, Manipulator, Appearance -> "Open", 
      AppearanceElements -> {"StepLeftButton", "StepRightButton"}}]
    

    关于 SO 的一个相当近期的问题解决了如何使用以下选项从一开始就为操纵器显示这些离散按钮:Appearance-> Open How to show the animation control by default.

    编辑: 您还可以指定要显示的离散按钮,例如只需使用

    的步骤左右按钮
    AppearanceElements -> {"StepLeftButton", "StepRightButton"}
    

    我添加到上面的代码示例中。

    实际上,一个更好更简单的选择是使用触发控件来隐藏滑块。

    Manipulate[
     Graphics[{Rectangle[{1, 1}, {3, 3}], Circle[{where, 2}, 1]}, 
      PlotRange -> {{0, 11}, {0, 3}}, ImageSize -> {300, 60}], {where, 1, 
      10, 1, ControlType -> Trigger, 
      AppearanceElements -> {"StepLeftButton", "StepRightButton"}}]
    

    【讨论】:

    • 谢谢。我的问题是,与其他形式的 Control 相比,我发现那些丑陋的东西。
    【解决方案3】:

    正如 Yoda 所示,Button 可用于 Manipulate 中的 NextPrevious 按钮。这种按钮通常用于通过有限范围的对象。在此范围的开头和结尾处,应分别禁用 Previous 和 Next 按钮。为了让它工作,可以使用按钮属性Enabled,因为它的动作依赖于一个交互变化的值,所以需要Dynamic。下面的玩具示例展示了它是如何工作的。

    示例代码:

    votePictures[picturesInput_] :=
     DynamicModule[{pictures = picturesInput, status, i},
      status = Table["Not Voted", {Length[pictures]}];
      i = 1;
      Panel[
       Row[
        {
         Dynamic[Show[pictures[[i]], ImageSize -> 256]], Spacer[72 0.7],
         Column[
           {
            Row[{Style["Status  ", FontFamily -> "Arial-Bold"], 
              SetterBar[
               Dynamic[status[[i]]], {"No response", "Ugly", "Nice"}]}
            ], , ,
            Row[{
               Button["Previous", i -= 1, Enabled -> (i > 1)], 
               Button["Next", i += 1, Enabled -> (i < Length[pictures])]}
            ] // Dynamic,
            Row[
                {
                 Style["Picture   ", FontFamily -> "Arial-Bold"], 
                 Slider[Dynamic[i], {1, Length[pictures], 1},Appearance -> "Labeled"]
                }
            ], , ,
            Button["Save results", (*Export code here *)]
            }
           ] // Framed
         }
        ], ImageSize -> 750
       ]
      ]
    
    pictures = ExampleData[#] & /@ ExampleData["TestImage"]
    
    votePictures[pictures]
    

    【讨论】:

    • @yoda 您在范围末端停止的方法可能更简单,但从人为因素的角度来看是不正确的。在给定时间无效的按钮应显示为灰色(取决于操作系统的隐喻),以免混淆用户。这是一种非常常见的设计模式。以我的示例中的“保存”按钮为例。如果用户在判断所有图片之前无法保存,则该按钮不应显示为活动状态。只是一点期望管理。在这里,我选择让这个按钮一直处于活动状态,表示您可以随时保存。
    • 对于那些感到困惑的人:Yoda 10 分钟前发表的上述评论在 a 写上述回复时消失了。我只是把回复留在这里,以防万一这是一个技术问题。
    • @Sjoerd:你绝对是正确的,我只是同意你的观点。我删除了,因为我写的内容不正确,编辑我的评论为时已晚:)
    • @Sjoerd,我有时间浏览你的代码,我希望很快就能轻松搞定。我相信您应该将此添加到您在 wolfram 上的演示中。它确实是一个。
    • @500 谢谢。 wrt演示:我相信我只贡献了一个想法,但我还没有提交我自己的一个。你指的是哪一个?
    猜你喜欢
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多