Saran
Saran

Reputation: 366

changing the color of button after some time tcl tk

I want to change the color of the radio button after some time. Here is the code i tried,

set field1 [radiobutton .field1 -disabledforeground green -state "disabled" ]
set field2 [radiobutton .field2 -disabledforeground red -state "disabled"]
set field3 [radiobutton .field3 -disabledforeground green -state "disabled"]

grid $field1 -row 0 -column 0
grid $field2 -row 0 -column 1
grid $field3 -row 0 -column 2

after 2000
$field1 configure -disabledforeground red
grid $field1 -row 0 -column 0

Window opens only after the changing the color.

How to see the change of color dynamically in run time?

Upvotes: 0

Views: 691

Answers (1)

Peter Lewerin
Peter Lewerin

Reputation: 13272

This seems to do the trick. I'm not sure if it's the best way, though.

set field1 [radiobutton .field1 -disabledforeground green -state disabled]
set field2 [radiobutton .field2 -disabledforeground red -state disabled]
set field3 [radiobutton .field3 -disabledforeground green -state disabled]

grid $field1 $field2 $field3

bind .field1 <Map> [list after 2000 {changeDisabledColor %W red}]

proc changeDisabledColor {w color} {
    $w configure -disabledforeground $color
}

A few thoughts about this:

  1. after 2000 means that the command sleeps for two seconds before returning, and during that time, the gui is unresponsive. after 2000 script means that the command returns immediately after scheduling an event to happen in two seconds.

  2. The Map event is triggered for a window when that window is added to the set of windows to display, but before it it's size and position are calculated. AFAICT it's a good event to latch onto if one wants to time some configuration to the appearance of the window.

  3. The handler for .field1 <Map> doesn't have to be a call to a command procedure: bind .field1 <Map> [list after 2000 {%W configure -disabledforeground red}] should work too. It's often a good idea to use a call like in my example, because that is usually more convenient to build on later.

Documentation: after, bind, grid, list, radiobutton, set

Upvotes: 4

Related Questions