TransWikia.com

How to use controls for creating static variables

Mathematica Asked on April 14, 2021

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.

One Answer

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.)

Correct answer by Michael E2 on April 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP