Reputation: 340
Just making a really simple Tcl/Tk program to learn, but I'm having a little trouble.
I need to make a button switch colours when clicked, so it will start red, when clicked it will turn green, and if clicked again will go back to red.
I have got it changing from red to green, but I don't know how to make it go back the other way. What is the best way to do this?
Upvotes: 2
Views: 4119
Reputation: 137707
It's not very hard (except on OSX, where this sort of thing is directly against platform UI guidelines) you just need to use a suitable callback:
button .b -background red -command [list toggleTheButton .b]
set state(.b) 1
proc toggleTheButton w {
global state
if {$state($w)} {
$w configure -background green
} else {
$w configure -background red
}
set state($w) [expr {!$state($w)}]
}
Consider using a checkbutton
instead when you need toggling, as it will be much more rapidly understood by the users.
Upvotes: 2