Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Button with incremented number
Hello,

for no special purpose (just curiosity as a TCL beginner) I created a
button that shows a number. With every click the number should be
incremented. My idea was this:

#! /usr/bin/wish
set i 1
button .b -text $i -command "incr i ; .b configure -text $i"
pack .b


But this doesn't work. Can anybody give me a hint?

Thanks,

Michael

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Pachta
06-01-05 02:04 PM


Re: Button with incremented number
Michael Pachta wrote:
> But this doesn't work. Can anybody give me a hint?

Sorry, forgot to write what doesn't work: the number gets incremented as
intended, but it won't get displayed as new button text. The initial "1"
remains.

Michael

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Pachta
06-01-05 02:04 PM


Re: Button with incremented number
Michael Pachta wrote:
> Hello,
>
> for no special purpose (just curiosity as a TCL beginner) I created a
> button that shows a number. With every click the number should be
> incremented. My idea was this:
>
> #! /usr/bin/wish
> set i 1
> button .b -text $i -command "incr i ; .b configure -text $i"
> pack .b

The button does change its label; it just happens to be to the same
value.  I think you want to delay the computation of the label to run
time, when you click on the button.  One way, with minimum change to
your code, is as follows:

% button .b -text $i -command "incr i ; eval .b configure -text \$\i"

In general, unless you have really simple calculations, the practice is
to call a proc.  Then, in that proc, you could do your processing.


Report this thread to moderator Post Follow-up to this message
Old Post
tunity5@yahoo.com
06-01-05 02:04 PM


Re: Button with incremented number
Michael Pachta <mipani@gmx.de> wrote:
> #! /usr/bin/wish
> set i 1
> button .b -text $i -command "incr i ; .b configure -text $i"
> pack .b

In a double-quoted string, $i gets substituted immediately,
so the thing that you really end up passing to -command is:
"incr i ; .b configure -text 1"

In your case you could just easily write:
button .b -text $i -command {incr i ; .b configure -text $i}
or
button .b -text $i -command "incr i ; .b configure -text \$i"
(just protect that one $ from being processed too soon)
and it will work.

There's a much easier way, too:
button .b -textvariable i -command {incr i}
with -textvariable, the button will track the value of
the variable automatically. Don't forget to use just i,
(not $i) for -textvariable, because as its name implies, it
wants a variable, not a value.


Report this thread to moderator Post Follow-up to this message
Old Post
Andreas Leitgeb
06-01-05 02:04 PM


Re: Button with incremented number
Andreas Leitgeb wrote:
> In a double-quoted string, $i gets substituted immediately,
> so the thing that you really end up passing to -command is:
>    "incr i ; .b configure -text 1"
>
> In your case you could just easily write:
>    button .b -text $i -command {incr i ; .b configure -text $i}

Thank you! That was really easy. Didn't know that the complete command
string gets processed (and all variable references will be substituted
at once when encountered by the interpreter). That's a bad trap.

> There's a much easier way, too:
>   button .b -textvariable i -command {incr i}
> with -textvariable, the button will track the value of
> the variable automatically.

Will use this feature next time. Thanks a lot!

Michael

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Pachta
06-01-05 09:01 PM


Re: Button with incremented number
Michael Pachta wrote:
> Andreas Leitgeb wrote:
> 
>
>
> Thank you! That was really easy. Didn't know that the complete command
> string gets processed (and all variable references will be substituted
> at once when encountered by the interpreter). That's a bad trap.
>

Not a bad trap; that's just the way the language is designed (and for
good reason, I'm sure you'll eventually discover). Substitution is
performed according to the rules on all arguments before giving them to
the commands for processing... and the rules say that things in "" get
substituted, things in {} don't.

Report this thread to moderator Post Follow-up to this message
Old Post
Bryan Oakley
06-01-05 09:01 PM


Re: Button with incremented number
Bryan Oakley wrote: 

> Not a bad trap; that's just the way the language is designed (and for
> good reason, I'm sure you'll eventually discover). Substitution is
> performed according to the rules on all arguments before giving them to
> the commands for processing... and the rules say that things in "" get
> substituted, things in {} don't.

The solution to my question was to use the curly brackets to prevent,
let's say, premature substitution (... -command {incr i ; .b configure
-text $i}). But substitution does happen. The button now shows 2,3,4
etc. and not the literal string $i. If I would write -text {$i} there
wouldn't be any substitution at all (as I would expect it).

But let's see what I learned:

1) "..." - immediate substitution of all variables
2) {...} - delayed subst. of variables
3) {...{$...}...} - no subst. of variables inside the inner braces

OK, I accept it and will consider it for later.

Thanks,

Michael

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Pachta
06-01-05 09:01 PM


Re: Button with incremented number
Michael Pachta <mipani@gmx.de> wrote:
> But let's see what I learned:
> 1) "..." - immediate substitution of all variables
With "variables" you surely mean those with a $ in front.
same goes for command-substitution [blabla ...]

> 2) {...} - delayed subst. of variables
delayed? it depends on what you do with that thing.

If you later execute it some way (-command, eval, if, while, proc, ...)
then yes, it will be substed delayed.  if you do not execute it
but perhaps only use it for -text, then it will be never substed.

All the rules about  "will be substed" or "will not be .." are
always focused on the step of putting together the thing to
execute. What happens during execution is beyond the scope
of these rules.

e.g.
set var {puts $foo}
if 1 {puts $var}

In both cases, no substitution happens *during parsing the command*,
but the if conmmand then does another round on its second argument,
so you will see the value of $var , namely 'puts $foo' printed out.

> 3) {...{$...}...} - no subst. of variables inside the inner braces
eval  {  if 1  {  puts  $var  }  }


Report this thread to moderator Post Follow-up to this message
Old Post
Andreas Leitgeb
06-02-05 02:03 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Tcl archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:42 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.