Message Boards Message Boards

GROUPS:

Make Button as an additional output of a Module?

Posted 1 month ago
442 Views
|
4 Replies
|
1 Total Likes
|

Hello.

I would like to make a Module, the returns a value and additionally it should also show a Button, with some more functionality. My try:

test[zahl_] := Module[
   {zahlM, zahlL},

   zahlM = ToBoxes[zahl];
   zahlL = StringJoin["$", ToString[TeXForm[zahl]], "$"];

   Print[Button[DisplayForm[zahlM], CopyToClipboard[zahlL]]];

   Return[zahl]
   ];

a = test[7/3];

The Problem is, that the the button doesn't work in the planned way. If I push the printed Button, the thing in the clipboard is (instead of the String saved in "zahlL"):

zahlL$18424

If I use only the Print-command and delete the Return[zahl], it works fine.

Is there a possibility, how to make work both, the Return and the Print in combination?

4 Replies
Posted 1 month ago
test[zahl_] := Module[{zahlM}, zahlM = ToBoxes[zahl];
      Print[Button[DisplayForm[zahlM], 
        CopyToClipboard[
         StringJoin["$", ToString[TeXForm[HoldForm@zahl]], "$"]]]];
      zahl]
Posted 1 month ago

The little program was just an example. You changed in way, that doesnt answer my question. The two variables in Button are both local defined. If possible, this shoudnt be changed. But if there is no such solution, I put the value of the local "zahl2" in the global "zahl3", then it works.

test[zahl_] := Module[
   {zahl1, zahl2},

   zahl1 = 5;
   zahl2 = 10;
   zahl3 = zahl2;

   Print[Button[zahl1, CopyToClipboard[zahl2]]];
   Print[Button[zahl1, CopyToClipboard[zahl3]]];

   Return[zahl];
   ];

What is the point of zahl and it being Returned? Here are few notes:

  • Module variables are Temporary, Button action may not be able to use them since the parent Module does not exist anymore.

  • If you want to scope variables within UI use DynamicModule but then:

  • don't use Print to create layout, use e.g. Column[{Button...}]. It will keep buttons within DynamicModule.

Posted 1 month ago

I found now a solution, that works great :) "With[...]" does exactly what I was lookiing for:

test[inNumber_] := Module[
   {outNumber, outMathematica, outLatex},

   outNumber = inNumber^2;
   outMathematica = SuperscriptBox[inNumber, 2];
   outLatex = StringJoin["$", ToString[inNumber], "^2$"];

   With[{outButton = outLatex},
    Print[Button[DisplayForm[outMathematica], Print[outButton]]]
    ];

   Return[outNumber];
   ];

@Kuba: Sorry, I didn't explain well my problem before. I didn't want to have dynamic content in the Button, only the content of a local variable at one time. But thx for the hints. My english is really bad, so its not so easy for me to say what I want to say ;)

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract