Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bind buttonpress

Status
Not open for further replies.

sl1200mk2

Technical User
Jun 7, 2005
39
FR
hi bong

i don't know how to set a variable to 100 on buttonpress and back to 0 on buttonrelease

if i put 2 bind:
bind . <ButtonPress-1> [set x 100]
bind . <ButtonRelease-1> [set x 0]

nothing happen when i click......

what do u think about that?
++
nico
 
I'm not an expert at Tk/Tcl, but aren't you supposed to use curly braces after the bind? bind . <ButtonPress-1> {set x 100}

-Bones
 
Bones3 is right. The curly braces delay substitution until the button is pressed.

_________________
Bob Rashkin
rrashkin@csc.com
 
hi all
if i put curly braces, x variable is no more available....
 
We may need to see more of your code, but try using set ::x ...

_________________
Bob Rashkin
rrashkin@csc.com
 
well, here's the lines

set 1 [lindex $sp 0]
set 2 [lindex $sp 1]

bind . <ButtonPress-1> {$Dlightsub::local.$1.[expr ([string map {0 {10}} $2] - 1)] set 100}


$Dlightsub::local.$1.[expr ([string map {0 {10}} $2] - 1)] is the name of the scale is want to set to 100 when i press the associated button and go back to 0 on release

++
nico
 
First of all, I think it's [red]very bad[/red] to use numbers as variable names. So, sidestepping that, I go back to an earlier thread where we laid out some simple scales. For each of those I'll put in a button that, when pressed, sets the value of the corresponding scale variable to 100:
#a frame for the controls
pack [frame .1 -borderwidth 4] -side top
#add the controls
pack [spinbox .1.s -textvariable circ -from 0 -to 512] -side left
pack [button .1.b -text go -command {MakeScales $circ}] -side left -padx 8

proc MakeScales {circ} {
#a frame for the scales
destroy .2
pack [frame .2 -borderwidth 4] -side top
set numRows [expr {$circ/10}]
set rmndr [expr {$circ%10}]
pack [frame .2.r] -side bottom
for {set i 0} {$i<$rmndr} {incr i} {
scale .2.r.s$i -variable subR$i
pack .2.r.s$i -side left
}
for {set r 0} {$r<$numRows} {incr r} {
pack [frame .2.$r] -side bottom
for {set i 0} {$i<10} {incr i} {
pack [scale .2.$r.$i -variable sub$r$i] -side left
}
}
[red]pack [frame .2.rr] -side top
for {set i 0} {$i<$rmndr} {incr i} {
pack [button .2.rr.$i -text subR$i -command "set subR$i 100"] -side left
}
for {set r 0} {$r<$numRows} {incr r} {
pack [frame .2.b$r] -side top
for {set i 0} {$i<10} {incr i} {
pack [button .2.b$r.$i -text "sub$r$i" -command "set sub$r$i 100"] -side left
}[/red]
}
}


Notice that you don't use ButtonPress for a button widget; that event is generated by a mouse button. You also don't need to bind the event to the button; it's already bound in the widget. The key here, though, is using "" rather than {}. Otherwise, the values of r and i will be substituted when the button is pressed instead of when it's instantiated.

_________________
Bob Rashkin
rrashkin@csc.com
 
ok for the number as name variables, i will change that

i've used "" instead of {} and it works, but, it still don't go back to 0 when i depress the button....

also i don't bind the buttonpress event to the button but to the scale...

thanks for the past tips (and the new ones)

nico
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top