Passing vars to buttons? 
Author Message
 Passing vars to buttons?

Once again, I am having difficulty with buttons.  Say that I have a
procedure that load s files.  It sets up a dialog window, with a label and
two buttons.  You type in a filename, then press one of the two buttons.
If the button is OK, then it loads the file.  All very routine.

The routine is called file.openFile.  It takes one paramater, an array to
store the opened file into.  The OK button calls a procedure with two
variables: the name of the file to open and the array to store data in.
The problem is, the -command option on buttons does not seem to pass on
information about variables in the procedure calling it.  For instance:

proc hello_dolly {
        set harf 17
        button .hello.dolly.OK -command {
                puts $harf
        }

Quote:
}

will give me the error "no such variable: $harf" or some such.  Is this
correct?  What can I do about it?  SOme sample code would be delicious...

Rusty...
...dedicated, yet frustrated tcl convert.



Sat, 15 May 1999 03:00:00 GMT  
 Passing vars to buttons?


: Once again, I am having difficulty with buttons.  Say that I have a
: procedure that load s files.  It sets up a dialog window, with a label and
: two buttons.  You type in a filename, then press one of the two buttons.
: If the button is OK, then it loads the file.  All very routine.

: The routine is called file.openFile.  It takes one paramater, an array to

by the way, I already tried something like putting a "global harf" in the
-command sequence.  no dice.



Sat, 15 May 1999 03:00:00 GMT  
 Passing vars to buttons?

Quote:

> proc hello_dolly {
>        set harf 17
>        button .hello.dolly.OK -command {
>                puts $harf
>        }
>}
> by the way, I already tried something like putting a "global harf" in the
> -command sequence.  no dice.

Close, the -command is executed in global context (i.e., not in the
context
of hello_dolly, which almost certainly has returned by the time the
button is pushed).  Any variable accessed there must be global.
So put the global in the hello_dolly proc itself to make harf
global.

An alternative would be to subsitute for harf in the procedure
hello_dolly
when you run the button command, rather than evaluating it at the time
the button is pushed (if harf is really a constant as far as the button
is concerned).  For example

        button .hello.dolly.OK -command "puts $harf"

-Ron



Sat, 15 May 1999 03:00:00 GMT  
 Passing vars to buttons?



Quote:
>    set harf 17
>    button .hello.dolly.OK -command { puts $harf }

Common mistake #1: Rules of substitution

The above should be:
        button .hello.dolly.OK -command [list puts $harf]

--
     Jeffrey Hobbs                       office: 541.683.7891

              URL: http://www.cs.uoregon.edu/~jhobbs/



Sat, 15 May 1999 03:00:00 GMT  
 Passing vars to buttons?

Quote:

> Once again, I am having difficulty with buttons.  Say that I have a
> procedure that load s files.  It sets up a dialog window, with a label and
> two buttons.  You type in a filename, then press one of the two buttons.
> If the button is OK, then it loads the file.  All very routine.

> The routine is called file.openFile.  It takes one paramater, an array to
> store the opened file into.  The OK button calls a procedure with two
> variables: the name of the file to open and the array to store data in.
> The problem is, the -command option on buttons does not seem to pass on
> information about variables in the procedure calling it.  For instance:

> proc hello_dolly {
>         set harf 17
>         button .hello.dolly.OK -command {
>                 puts $harf
>         }
> }

> will give me the error "no such variable: $harf" or some such.  Is this
> correct?  What can I do about it?  SOme sample code would be delicious...

Yes, that had me fooled for a bit.  The problem is that the command for
the button is parsed when the button is pressed, at which time `harf'
doesn't exist, not being global.  I usually fix this with the `list'
command:

         set harf 17
         button .hello.dolly.OK -command { [list puts $harf] }

This forces `harf' to be evaluated when the `button' command is parsed.

--

Software Engineering Group,             Tel: +44-1223-374000
Royal Greenwich Observatory             Fax: +44-1223-374700



Mon, 17 May 1999 03:00:00 GMT  
 Passing vars to buttons?


Quote:
>I usually fix this with the `list'
>command:

>         set harf 17
>         button .hello.dolly.OK -command { [list puts $harf] }

>This forces `harf' to be evaluated when the `button' command is parsed.

not with those braces around, it doesn't.


Mon, 17 May 1999 03:00:00 GMT  
 Passing vars to buttons?

Quote:


>> Once again, I am having difficulty with buttons.  Say that I have a
>> procedure that load s files.  It sets up a dialog window, with a label and
>> two buttons.  You type in a filename, then press one of the two buttons.
>> If the button is OK, then it loads the file.  All very routine.

>> The routine is called file.openFile.  It takes one paramater, an array to
>> store the opened file into.  The OK button calls a procedure with two
>> variables: the name of the file to open and the array to store data in.
>> The problem is, the -command option on buttons does not seem to pass on
>> information about variables in the procedure calling it.  For instance:

>> proc hello_dolly {
>>         set harf 17
>>         button .hello.dolly.OK -command {
>>                 puts $harf
>>         }
>> }

>> will give me the error "no such variable: $harf" or some such.  Is this
>> correct?  What can I do about it?  SOme sample code would be delicious...
>Yes, that had me fooled for a bit.  The problem is that the command for
>the button is parsed when the button is pressed, at which time `harf'
>doesn't exist, not being global.  I usually fix this with the `list'
>command:
>         set harf 17
>         button .hello.dolly.OK -command { [list puts $harf] }
>This forces `harf' to be evaluated when the `button' command is parsed.

Nope. Skip the braces, and it will.

                /Mikko

Quote:
>--

>Software Engineering Group,         Tel: +44-1223-374000
>Royal Greenwich Observatory                 Fax: +44-1223-374700

--

 DynaSoft


Mon, 17 May 1999 03:00:00 GMT  
 Passing vars to buttons?


Quote:

>> Once again, I am having difficulty with buttons.  Say that I have a
>> procedure that load s files.  It sets up a dialog window, with a label and
>> two buttons.  You type in a filename, then press one of the two buttons.
>> If the button is OK, then it loads the file.  All very routine.

>> The routine is called file.openFile.  It takes one paramater, an array to
>> store the opened file into.  The OK button calls a procedure with two
>> variables: the name of the file to open and the array to store data in.
>> The problem is, the -command option on buttons does not seem to pass on
>> information about variables in the procedure calling it.  For instance:

>> proc hello_dolly {
>>         set harf 17
>>         button .hello.dolly.OK -command {
>>                 puts $harf
>>         }
>> }

>> will give me the error "no such variable: $harf" or some such.  Is this
>> correct?  What can I do about it?  SOme sample code would be delicious...

>Yes, that had me fooled for a bit.  The problem is that the command for
>the button is parsed when the button is pressed, at which time `harf'
>doesn't exist, not being global.  I usually fix this with the `list'
>command:

>         set harf 17
>         button .hello.dolly.OK -command { [list puts $harf] }

>This forces `harf' to be evaluated when the `button' command is parsed.

Or if harf is something that changes in value, can't you have:

global harf
set harf 17
button ... -command { puts $harf }

- frank

--

*  Maybe it's that we are all outsiders, we are all making our own   *
* unusual way through a wilderness of normality that is just a myth. *
***** - Anne Rice ******** http://www.csclub.uwaterloo.ca/~fyao ******



Mon, 17 May 1999 03:00:00 GMT  
 Passing vars to buttons?

Quote:


> >I usually fix this with the `list'
> >command:

> >         set harf 17
> >         button .hello.dolly.OK -command { [list puts $harf] }

> >This forces `harf' to be evaluated when the `button' command is pars

Use subst to force substitutions. A lot more general usage.

button .hello.dolly.OK -command  [subst {puts $harf} ]



Tue, 18 May 1999 03:00:00 GMT  
 Passing vars to buttons?


Quote:
> An alternative would be to subsitute for harf in the procedure
> hello_dolly when you run the button command, rather than evaluating
> it at the time the button is pushed (if harf is really a constant as
> far as the button is concerned).  For example

>     button .hello.dolly.OK -command "puts $harf"

This leads to a very subtle error which only becomes visible if harf
contains characters with significance to Tcl (such as space, brackets,
dollar, etc).  The key is that you are building a string here, and not
a list.  Commands are lists, and so should be built with the list
command (if you have more than one command to execute in this binding,
use a procedure, as it will save you a _lot_ of trouble in the long
run).

    button .hello.dolly.OK -command [list puts $harf]

Donal.
--
Donal K. Fellows   http://r8h.cs.man.ac.uk:8000/  (SAY NO TO COMMERCIAL SPAMS!)


 +-> ++44-161-275-6137  Send correspondence to my office  ++44-1274-401017 <-+



Tue, 18 May 1999 03:00:00 GMT  
 Passing vars to buttons?


Quote:

>>> I usually fix this with the `list' command:
>>>     set harf 17
>>>     button .hello.dolly.OK -command { [list puts $harf] }
>>> This forces `harf' to be evaluated when the `button' command is pars

> Use subst to force substitutions. A lot more general usage.

> button .hello.dolly.OK -command  [subst {puts $harf} ]

You will come- amusingly -a-cropper when harf happens to have a space
in it.  [list puts $harf]  is the correct way of producing the command
in _all_ circumstances (especially without the curlies round it... :^)

Donal.
--
Donal K. Fellows   http://r8h.cs.man.ac.uk:8000/  (SAY NO TO COMMERCIAL SPAMS!)


 +-> ++44-161-275-6137  Send correspondence to my office  ++44-1274-401017 <-+



Tue, 18 May 1999 03:00:00 GMT  
 Passing vars to buttons?

[snip]

Quote:

> proc hello_dolly {
>         set harf 17
>         button .hello.dolly.OK -command {
>                 puts $harf
>         }
> }

> will give me the error "no such variable: $harf" or some such.  Is this
> correct?  What can I do about it?  SOme sample code would be delicious...

The explanation is simple: button commands run in the global context, so
when the button is pressed it is looking for a global variable named
"harf". Since harf was defined in a procedure, it's not a global
variable. Hense, the button command can't find it.

There is no way for a global procedure (ie: a button command) to access
local data. So, to do what you want you must make "harf" global, like
so:

proc hello_dolly {
    global harf
    set harf 17
    button .hello.dolly.OK -command {
            puts $harf
    }

Quote:
}

--

Software Engineer                http://www1.clearlight.com/~oakley/
Healthcare Communications, Inc.  http://www.healthcare.com/


Sat, 22 May 1999 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. unknown vars in button-command

2. passing shell vars to gawk

3. passing vars to shell script

4. passing vars to awk script

5. Passing and getting vars. from C to awk

6. HELP: Passing Unix vars to AWK

7. passing run-time vars to RRUN

8. Accessing Vars in Group Passed by Address

9. How pass compound vars to subroutine?

10. vars passing through URL

11. Newbie - passing variable number of vars to expect script

12. Pass vars b/w programs?

 

 
Powered by phpBB® Forum Software