Re: pasing parametersfor a function when a button is used



mailme.neena@xxxxxxxxx wrote:
On Feb 27, 10:27 am, billpo...@xxxxxxxxxxxx wrote:
On Feb 26, 9:13 pm, mailme.ne...@xxxxxxxxx wrote:

Hi,
   Can someone help me in finding how to pass parameters to a function
when a particular button is clicked.I need to send the value of a
variable as a parameter to the functin.The error it gives me..is
unidentified variable..when the variable is preceded bya dollar
sign.But it is able to identify when the variable is given just like
that..(sans dollar symbol).Since i require the value of the variable
to be passed,the dollar symbol is inevitable.I had given,

button $bf.ok -text Generate -command { AST::Generated $bb} \
-bd 1 -activebackground grey {Arial 10}

When a command is executed in response to a button push, it is
evaluated in the global scope. Is your variable bb a global?

Thanx a lot.I just declared the variable globally and now its working
perfectly as expected.
thanx :)

You might want to read the syntax rules for Tcl.
http://www.tcl.tk/man/tcl8.5/TclCmd/Tcl.htm

Instead of making bb a global you could do this:

button $bf.ok -text Generate -command [list AST::Generated $bb]

Then when the button code evaluates the -command argument after being
pressed, it will be a proper list.

You may see some less desirable code that does:

button $bf.ok -text Generate -command "AST::Generated $bb"

That code isn't a good idea, because of the way quote substitution
works.

For example:
$ wish8.5
% set var "Hello World"
Hello World
% proc callback val { puts $val }
% pack [button .b1 -text 1 -command [list callback $var]]
% pack [button .b2 -text 2 -command "callback $var"]


If you press button .b2 you get an error message that states:
wrong # args: should be "callback val"
while executing
"callback Hello World"
invoked from within
".b2 invoke"

..b1 works fine when pressed.


It's this syntax issue that leads many to gross hacks with trying to
escape certain characters and essentially do what list -> string type
conversion does for you automatically. Many people that dislike Tcl
don't understand this in my experience. I've seen some blog postings
very critical of this, that are actually misunderstandings.


George
.