是否可以调整TogglerBar的大小/字体,以便在名称大小不同时它们都同样大。
以下示例是Belisarius提出的解决方案:"Can TogglerBar be used as multiple CheckBox in Mathematica ?"
我希望每个Button的大小相同。
Manipulate[Graphics[
{
{White, Circle[{5, 5}, r]},(*For Mma 7 compatibility*)
If[MemberQ[whatToDisplay, "I am a Circle"],
{Red, Circle[{5, 5}, r]}],
If[MemberQ[whatToDisplay, "and I am a very nice Square"], {Blue,
Rectangle[{5, 5}, {r, r}]}],
If[MemberQ[whatToDisplay, "Other"], {Black,
Line[Tuples[{3, 4}, 2]]}]
},
PlotRange -> {{0, 20}, {0, 10}}
],
{{r, 1, Style["Radius", Black, Bold, 12]}, 1, 5, 1,
ControlType -> Slider,
ControlPlacement -> Top},
Control@{{whatToDisplay, True,
Style["What", Black, Bold, 12]}, {"I am a Circle",
"and I am a very nice Square", "Other"},
ControlType -> TogglerBar,
Appearance -> "Horizontal",
ControlPlacement -> Top}]
编辑:代码中非常丑陋(如果我们仍然可以调用该代码)但显示效果不错。
答案 0 :(得分:5)
Mathematica 8介绍了Overlay
,它允许多个表达式轻松地叠加在彼此之上。加上Invisible
(来自第6版),我们拥有所有必要的成分,可以轻松地同步所有按钮尺寸,而无需计算空间或像素。以下代码说明了它们的用法,并强调了相关部分:
DynamicModule[{options, allLabels, size, pad}
, options =
{ "I am a Circle" -> {Red, Circle[{5, 5}, size]}
, "and I am a very nice Square" -> {Blue, Rectangle[{5, 5}, {size, size}]}
, "Other" -> {Black, Line[Tuples[{3, 4}, 2]]}
}
; allLabels = options[[All, 1]]
; pad[label_] :=
Overlay[Append[Invisible /@ allLabels, label], Alignment -> Center]
; Manipulate[
Graphics[what /. options /. size -> r, PlotRange -> {{0, 20}, {0, 10}}]
, {{r, 1, "Radius"}, 1, 5, 1}
, {{what, {}, "What"}, # -> pad[#] & /@ allLabels, TogglerBar}
]
]
定义:
pad[label_] :=
Overlay[Append[Invisible /@ allLabels, label], Alignment -> Center]
定义了一个函数,它将所有标签无形地堆叠在一起,然后将所需的标签明显放在其上。结果是所需的标签,两侧有足够的空白,以容纳任何其他标签。
Manipulate
语句使用pad
为TogglerBar
创建标签:
{{what, {}, "What"}, # -> pad[#] & /@ allLabels, TogglerBar}
请注意,标签以value -> label
形式指定,以允许值保留其原始格式,而Overlay
和Invisible
说明符不会pad
添加。
答案 1 :(得分:4)
这是一种方法,虽然不完全令人满意,因为不会自动计算面板长度。我们称之为第一种方法......
Manipulate[
Graphics[{{White, Circle[{5, 5}, r]},(*For Mma 7 compatibility*)
If[MemberQ[whatToDisplay, "I am a Circle"], {Red, Circle[{5, 5}, r]}],
If[MemberQ[whatToDisplay, "and I am a very nice Square"],
{Blue, Rectangle[{5, 5}, {r, r}]}],
If[MemberQ[whatToDisplay, "Other"], {Black, Line[Tuples[{3, 4}, 2]]}]},
PlotRange -> {{0, 20}, {0, 10}}],
{{r, 1, Style["Radius", Black, Bold, 12]}, 1, 5, 1, ControlType -> Slider,
ControlPlacement -> Top},
Control@{{whatToDisplay, True, Style["What", Black, Bold, 12]},
(# -> Panel[#, ImageSize -> 150, FrameMargins -> 0,
Background -> White,
Alignment -> Center]) & /@
{"I am a Circle", "and I am a very nice Square", "Other"},
ControlType -> TogglerBar,
Appearance -> "Horizontal",
ControlPlacement -> Top}]
修改
这里有一个更好的方法,自动大小计算:
(* get the Image Size first*)
ley = {"I am a Circle", "and I am Square", "Other"};
sZ = Max[Dimensions[ImageData[Rasterize[#][[1]]]][[2]] & /@ ley];
Manipulate[
Graphics[
{{White, Circle[{5, 5}, r]},(*For Mma 7 compatibility*)
If[MemberQ[whatToDisplay, "I am a Circle"],
{Red, Circle[{5, 5}, r]}],
If[MemberQ[whatToDisplay, "and I am a very nice Square"],
{Blue, Rectangle[{5, 5}, {r, r}]}],
If[MemberQ[whatToDisplay, "Other"],
{Black, Line[Tuples[{3, 4}, 2]]}]},
PlotRange -> {{0, 20}, {0, 10}}],
(*Controls Follow *)
{{r, 1, Style["Radius", Black, Bold, 12]}, 1, 5, 1,
ControlType -> Slider,
ControlPlacement -> Top },
Control@{{whatToDisplay, True, Style["What", Black, Bold, 12]},
(# -> Panel[#, ImageSize -> sZ,
FrameMargins -> 0,
Background -> White,
Alignment -> Center]) & /@ ley,
ControlType -> TogglerBar,
Appearance -> "Horizontal",
ControlPlacement -> Top}]
答案 2 :(得分:3)
Mathematica可能会在某处做出选择,但我不知道。所以,这是一个建议的解决方案:
首先,它左右填充一个字符串,使其长度为n
:
Clear[strpad];
strpad[str_String, n_] := Module[
{strlength, exc, excr},
strlength = StringLength@str;
exc = Floor[(n - strlength)/2];
excr = n - strlength - exc;
StringJoin[
Table[" ", {i, exc}],
str,
StringJoin[Table[" ", {i, excr}]]]]
然后,修改您的程序,以便您使用这些填充字符串或在Rule[#, strpad[#, 30]] & /@
之前添加{"I am a Circle", "and I am a very nice Square", "Other"}
,以便您的代码成为
Manipulate[
Graphics[{{White, Circle[{5, 5}, r]},(*For Mma 7 compatibility*)
If[MemberQ[whatToDisplay, "I am a Circle"], {Red,
Circle[{5, 5}, r]}],
If[MemberQ[whatToDisplay, "and I am a very nice Square"], {Blue,
Rectangle[{5, 5}, {r, r}]}],
If[MemberQ[whatToDisplay, "Other"], {Black,
Line[Tuples[{3, 4}, 2]]}]},
PlotRange -> {{0, 20}, {0, 10}}], {{r, 1,
Style["Radius", Black, Bold, 12]}, 1, 5, 1, ControlType -> Slider,
ControlPlacement -> Top},
Control@{{whatToDisplay, True, Style["What", Black, Bold, 12]},
Rule[#, strpad[#, 30]] & /@ {"I am a Circle", "and I am a very nice Square", "Other"},ControlType -> TogglerBar, Appearance -> "Horizontal",
ControlPlacement -> Top}]