On Jun 26, 2007, at 7:44 AM, Valen Onish wrote:

> I have:
> 1. $lb_1 = TkLabel.new(root){..}       - label
> 2. bt_toss=TkButton.new(root){..}      - button
>
> I want when I press the button:         1. label's background is  
> changed to blue
>                                         2. display changed label's  
> background
>                                         3. sleep(0.5)
>                                         4. label's background is  
> changed to green
>                                         5. display changed  label's  
> background
>
> but I see only the last colour.

You only see the last color because you don't call update on the  
label widget after you change the background color.

> here the code:
>
> require 'tk'
>
> root = TkRoot.new { title "Ruby/Tk  Example" }
>
> $lb_1 = TkLabel.new(root){
> background "red"
> foreground "blue"
> text "Your area"
> font "system,12"
> place('relx'=>0.4, 'rely'=>0.08)
> }
>
> bt_toss=TkButton.new(root){
> text "Toss"
> command proc{change_colour}
> place('relx'=>0.2, 'rely'=>0.78)
> }
>
> def change_colour
>   $lb_1.configure('background'=>'blue')
>   sleep(0.5)
>   $lb_1.configure('background'=>'green')
>   sleep(0.5)
>   $lb_1.configure('background'=>'gray')
>
> end
>
> Tk.mainloop

I strongly urge you to avoid using global variables to solve a  
scoping problem when there are better ways to solve the problem. I've  
taken the liberty to show you one way to avoid using a global name  
for the label widget. And I have Ruby-ized your code in a couple of  
other small ways.

<code>
require 'tk'

ZZZ = 2.0 # longer delay makes color changes easier to see

root = TkRoot.new { title "Ruby/Tk Example" }

lbl = TkLabel.new(root) {
    background "red"
    foreground "blue"
    text "Your area"
    # bigger font just to make text easier to see
    font "system, 24"
    place('relx' => 0.2, 'rely' => 0.08)
}

def lbl.change_colour
    # this is a singleton method; self is now the TkLabel object
    # also Ruby/Tk will build Tk configure calls for you
    background('blue')
    update # this makes the color change happen on the screen
    sleep(ZZZ)
    background('green')
    update
    sleep(ZZZ)
    background('gray')
end

# The local variable 'lbl' is visible in the black because
# the block is a closure.
callback = lambda { lbl.change_colour }

TkButton.new(root) {
    text "Toss"
    command callback
    place('relx' => 0.35, 'rely' => 0.8)
}

Tk.mainloop
</code>

Regards, Morton