1
$\begingroup$

The following segment of code is part of a project I am making and therefore I cannot show all the code. I hope the one below is enough.

The input is

Panel[DynamicModule[var, InputField[Dynamic[var], String]]]

while the output is

enter image description here

which in thid case displays DynamicModule[var,] that is not wanted. How would I fix this? I want to add that I want to keep the same functions used above.

Also, how would I put text next to the input field? Thank you.

$\endgroup$
6
  • 4
    $\begingroup$ Use DynamicModule[{var}, ...] instead. $\endgroup$
    – Carl Woll
    Jan 8, 2018 at 19:08
  • $\begingroup$ @CarlWoll this does seem to fix the problem but now when I enter a word and press enter nothing happens. Instead, it should evaluate the command as it did without this fix. $\endgroup$
    – DMH16
    Jan 8, 2018 at 19:15
  • 1
    $\begingroup$ @DMH16 It still does but if you need the variable outside of DynamicModule you will need an assignment in Dynamic[globVar = var] $\endgroup$
    – Coolwater
    Jan 8, 2018 at 19:19
  • 1
    $\begingroup$ @DMH16 Dynamic[var, (var = globVar = #) &] works, but I don't understand why the Dynamic[globVar = var] doesn't :/ $\endgroup$
    – Coolwater
    Jan 8, 2018 at 19:50
  • 1
    $\begingroup$ @Coolwater because Set is not Flat and (x = y) = 5 is not the same as x = y = 5 $\endgroup$
    – Kuba
    Jan 8, 2018 at 20:23

2 Answers 2

1
$\begingroup$
Panel[DynamicModule[{var = "Type here"},
  InputField[Dynamic[var], String]]]

or, for a version with a variable text that can be inspected

Panel[DynamicModule[{var = "Type here"},
  InputField[Dynamic[var, (text = var = #) &], String],
  Initialization :> (text = var)]]
$\endgroup$
1
$\begingroup$

I can't seem to add a comment above. Try this... Also, note that Modules and Dynamic Modules are like Objects and the variables such as 'var' is only valid inside the Module

Panel[DynamicModule[{var}, Grid[{{InputField[Dynamic[var], String],Dynamic[var]}}]]]

We can just include Panel inside the Module

DynamicModule[{var}, Panel[InputField[Dynamic[var], String],Dynamic[var]]]

All you do is tab whenever you fill the input field

$\endgroup$
0

Your Answer

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

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