当FieldSize不够时,如何使PopupMenu中的项目更紧密(更少的空白区域)

Nas*_*ser 2 wolfram-mathematica

我正在尝试格式化PopupMenu,并注意到当我创建FieldSize-> 7时,菜单中的一个项目将换行到换行符,即使在我看来有足够的白色空间它可以使用在右边.

当我增加FieldSize-> 8时,现在fieldSize已经足够了,并且没有任何菜单项会环绕.

但在我看来,现在浪费了大量的白色空间.我为PopupMenu尝试了很多选项,比如ContentPadding,这样可以使项目更紧凑,但是当我将fieldSize设置为7时,它坚持要包装那一项.

可能是字体样式问题?我想知道是否有人可能有这个伎俩.

这是代码,并显示问题

Manipulate[
opt,

Control[{ {opt,0,Style["display",11]},
  {0->Text@Style["bob only",Small],
  4->Text@Style["L + bob",Small],
  1->Text@Style["L only",Small],
  2->Text@Style["L resolved",Small],
  3->Text@Style["L + resolved",Small],
  5->Text@Style["L resolved + bob",Small],
  6->Text@Style["show all above",Small]        
},
ControlType->PopupMenu,FieldSize->7,
ImageMargins->0,
FrameMargins->0,ContentPadding->True,ImageSize->Small}]

]
Run Code Online (Sandbox Code Playgroud)

这是我打开菜单时的屏幕截图

在此输入图像描述

所以,现在我改变FieldSize-> 8,现在它看起来像这样:

在此输入图像描述

但上述通知,我认为这是在右边太多的空白,并FieldSize-> 7应该已经足够宽了该项目,以没有缠?

我尝试了FontFamily - >"Times"和"Courier",效果相同:

Manipulate[opt,
Control[{ {opt,0,Style["display",11]},
{0->Text@Style["bob only",Small,FontFamily->"Courier"],
4->Text@Style["L + bob",Small,FontFamily->"Courier"],
1->Text@Style["L only",Small,FontFamily->"Courier"],
2->Text@Style["L resolved",Small,FontFamily->"Courier"],
3->Text@Style["L + resolved",Small,FontFamily->"Courier"],
5->Text@Style["L resolved + bob",Small,FontFamily->"Courier"],
6->Text@Style["show all above",Small,FontFamily->"Courier"]
},ControlType->PopupMenu,FieldSize->7,
ImageMargins->0,FrameMargins->0,ContentPadding->True,ImageSize->Small}]
]
Run Code Online (Sandbox Code Playgroud)

实际上现在有了Courier,我不得不去FieldSize-> 9那个字段现在还没有环绕.所以Courier的表现最差.

是否有可能使场地比现在更紧张?

编辑1:回复MrWizard: 在此输入图像描述 在此输入图像描述

编辑2:

使用FieldSize-> Automatic,使整个菜单大小根据所选项目更改大小.

Manipulate[opt,Control[{{opt,0,Style["display",11]},
{0->Style["bob only",Small],
1->Style["L + bob",Small],
2->Style["L only",Small],
3->Style["L resolved",Small],
4->Style["L + resolved",Small],
5->Style["L resolved + bob",Small],
6->Style["show allabove",Small]},
ControlType->PopupMenu,FieldSize->Automatic,
ImageMargins->0,FrameMargins->0,ImageSize->Small}]]
Run Code Online (Sandbox Code Playgroud)

更新12月21日凌晨1点

此图初级讲座演示如何使用列昂尼德宏法,使其更轻松地构建UI和宏的使用减少UI对象的定制代码的重复.我目前大量使用这种方法,并且具有超过4,000行的复杂UI,仅用于UI本身,我不能用旧方法完成这项工作,因为我只是失去对事物的控制.

宏使其更易于管理的代码布局,更重要的是,现在我可以通过编辑而不是大码的宏代表小代码很容易改变UI本身.没有GUI构建器,宏是下一个最好的东西:)

在此输入图像描述

这是上面的代码

Manipulate[

 Row[{opt1, opt2}],

 Evaluate@With[{
    popUpMenu1Options = MenuStyle -> {Red},
    popUpMenu2Options = MenuStyle -> {Black},
    popUpMenuCommonOptions = {BaseStyle -> 20, ImageMargins -> 0, 
      FrameMargins -> 0, ImageSize -> All}
    },

   With[{

     menu1 = PopupMenu[Dynamic[opt1],
       {"A", "B"
        }, Sequence[popUpMenu1Options, popUpMenuCommonOptions]
       ],
     menu2 = PopupMenu[Dynamic[opt2],
       {"C", "D"
        }, Sequence[popUpMenu2Options, popUpMenuCommonOptions]
       ]
     },

    Row[{menu1, menu2}]

    ]
   ],

 {{opt1, "A"}, None},
 {{opt2, "D"}, None},
 TrackedSymbols :> {opt1, opt2}
 ]
Run Code Online (Sandbox Code Playgroud)

Dr.*_*ius 6

基于Simon的回答稍作修改可以更好地进行:

Manipulate[opt, 
 Control[{{opt, 0, Style["display", 11]}, 
   {0 -> Style["bob only", Small],
    1 -> Style["L only", Small],  
    4 -> Style["L + bob", Small],
    2 -> Style["L resolved", Small], 
    3 -> Style["L + resolved", Small], 
    5 -> Style["L resolved + bob", Small], 
    6 -> Style["show all above", Small]}, 
 ControlType -> PopupMenu, 
 ImageMargins -> 0, 
 FrameMargins -> 0, 
 ImageSize -> All}]]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 你觉得如何.那么,诀窍是使用ImageSize-> All?这是我从你拥有的东西和我拥有的东西中看到的唯一区别.我用过ImageSize-> Small.这很好.从来没有想过要考虑查看ImageSize选项,因为我总是使用Small来设置Manipulate控件.谢谢 (2认同)