2
$\begingroup$

I try to create a static variable initialized by some control. Let's create PopupMenu control

SomeText = "SomeText";
PopupMenu[Dynamic[PS], {0 -> "Prefix", 1 -> "Suffix"}]

Then, on the base of the control, a variable StringForLater is initialized

StringForLater = Dynamic[If[PS == 0, StringJoin["Prefix", SomeText], StringJoin[SomeText, "Suffix"]]]

Later StringForLaterperforms as a dynamical variable, but I need another behavior: after selecting an item in PopupMenu make StringForLater static, not dynamic.

$\endgroup$

1 Answer 1

2
$\begingroup$

I think this is what you want. You can use the tracking function argument to Dynamic[]:

SomeText = "SomeText"; (* I avoid starting vars with caps; so someText *)
PopupMenu[
 Dynamic[PS,
  (PS = #;
    StringForLater =   (* Ditto: stringForLater instead of StringForLater *)
     If[PS == 0, StringJoin["Prefix", SomeText], 
      StringJoin[SomeText, "Suffix"]]
    ) &],
 {0 -> "Prefix", 1 -> "Suffix"}]

Dynamic@StringForLater

(Avoiding initial capitals means my symbols won't conflict with built-in symbols.)

$\endgroup$
1
  • $\begingroup$ Thank you. Indeed, It works as needed. I'm surprised that Mathematica needs some tricks in order to implement simple things :(( $\endgroup$
    – Konstantin
    Commented Dec 1, 2020 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.