0
$\begingroup$

I'm trying to make a units palette to help with my various engineering courses. I thought about creating a "meta-palette" of sorts, maybe one that changes on the fly, or one where I can create all the buttons, arrange them on a grid and then export as a palette.

The first major hurdle I encountered and the issue at hand is that I can't for the life of me find a way to make the button's content "stick". Either the pasted content is left unevaluated, or it changes along with the InputField or just plain out throws an error. I have tried with and without many, if not all, combinations of Dynamic and non-Dynamic versions of Module, Setting, Evaluate, Defer, and more.

Here's the snippet of my code:

Quantity; Module[{QN = {list, of, all units}
 DynamicModule[{U = "", N = ""}, 
  Grid@{{"Button:", 
     Quiet[PasteButton[Dynamic@N, Quantity[None, U (*Herein lies the issue*)]]]}, {"Label:", 
     InputField[Dynamic@N, String, 
      ContinuousAction -> True]}, {"Unit:", 
     InputField[Dynamic@U, String, ContinuousAction -> True, 
      FieldCompletionFunction ->  
          Select[QN, 
           StringContainsQ[#, IgnoreCase -> True]] &)]}}]]

I trimmed some unnecessary strings to reduce bulk.

The intended behavior of that button is to input the unit typed in the bottom text box. After this I might want to duplicate the button as a "static" one so that it can be reused later, but I think that solving this issue would simplify that goal.

I hope you guys can help me figure this one out, thanks in advance!

$\endgroup$

1 Answer 1

1
$\begingroup$

Unlike Button the PastButton does not have HoldRest attribute. You can rewrite it then:

Button[Dynamic@N, NotebookApply[InputNotebook[], Quantity[None, U ]] ]

Does it fit your needs?

$\endgroup$
4
  • $\begingroup$ It does work, however it leaves the pasted data unevaluated, I fixed it by modifying it to: Button[Dynamic@N, NotebookApply[InputNotebook[], Quantity[None, U], All]; SelectionEvaluate@InputNotebook[]]. I has a bit of a glitchy behavior if clicked while the cursor is set outside a cell, but I think I can live with that. $\endgroup$ Mar 10, 2021 at 17:47
  • $\begingroup$ Also, I just tried to convert it to a palette and it doesn't work; either I can't type in the InputFields or it pastes the data into the palette's fields, instead of the working notebook. I'll try to look for an answer or, if needed, open a new question. $\endgroup$ Mar 10, 2021 at 18:01
  • $\begingroup$ @juanmanuel i fixed what you asked for. Please describe what is your end goal in detail and what exactly does not work. $\endgroup$
    – Kuba
    Mar 10, 2021 at 20:54
  • $\begingroup$ You can ignore my second comment. What I wanted to paste is not just the expression Quantity[None,U] but the result as if you evaluated it in place, so in the notebook it looks like just kg, for example. That's what the first comment's tweak achieves. $\endgroup$ Mar 11, 2021 at 4:12

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.