MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Silly Mathematica button question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36159] Re: [mg36139] Silly Mathematica button question
  • From: Omega Consulting <omega_consulting at yahoo.com>
  • Date: Fri, 23 Aug 2002 21:34:50 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

At 11:25 PM 8/22/2002, Lawson English wrote:
>Normally I don't feel so stupid, but I'm trying to create an interactive
>Mathematica notebook, and I'm stuck at square one.
>
>Specifically, how do I do something like create a button to add a number (or
>perform other mathematical functions) and then display or otherwise
>manipulate the result (eventually I want to be able to click a button that
>increments/decrements an angle and animate the resulting transformation of a
>vector, with an eye to finally simulating a simple robotic arm complete with
>simple controls to manipulate the arm).
>
>I want to have a variable (or matrix or whatever) defined as a global
>variable x, and then perform x = x+1 when a button is clicked. I've been
>using and programming computers for almost 25 years and I can't follow
>Wolfram's documentation. Is it just me, or is he always this obtuse when
>explaining things? I mean, given the amazing power of the Mathematica
>system, a sample list of buttons in a notebook that you could select and
>examine how they were implemented, would have been nice. I can't find such a
>list, and this seems to be par for the course for the rest of the
>documentation as well.

Button programming can be very confusing at first. It has a whole series of 
quirks that make it in many ways unique to Mathematica and programming in 
general. This causes a lot of head-scratching, but once you understand 
what's going on things get easier.

The key is to create a button that uses the kernel (by default it only uses 
the front end). Here's a simple example to help you get started.

In[1]:= x=1;

In[2]:=ButtonBox["Increment x",Active->True, ButtonEvaluator->Automatic,
     ButtonFunction:>Print["x = ",++x]
     ]//DisplayForm

Some random things to note:
- ButtonEvaluator->Automatic. This says use the kernel to implement the 
ButtonFunction.
- Buttons only create side-effects. They generate no output. What you see 
when you press the button is the result of 2 side effects. One from ++, 
which changes the value of x. The other from Print, which creates a cell.
- Print is, in general, a poor side-effect to use in a button. It's 
difficult to control where the Print cell is placed. It is worth your while 
to learn how to use other front end side-effect functions (such as 
NotebookRead and NotebookWrite) when using buttons.

Here are some further resources you might find helpful:
http://support.wolfram.com/mathematica/interface/buttons/
http://www.mathsource.com/Content/General/Tutorials/Programming/0209-809
http://library.wolfram.com/conferences/devconf99/hinton/Buttons19991022.nb
http://library.wolfram.com/conferences/devconf2001/horton2/horton2.nb

--------------------------------------------------------------
Omega Consulting
"The final answer to your Mathematica needs"

Spend less time searching and more time finding.
http://www.wz.com/internet/Mathematica.html



  • Prev by Date: Re: Silly Mathematica button question
  • Next by Date: another question about pattern replacements
  • Previous by thread: Re: Silly Mathematica button question
  • Next by thread: Re: Silly Mathematica button question