TkVarible question

Hi,

I am very green at Ruby and Ruby/Tk (just started working with it last
week, and love it so far)

I finally found a solution to the problem I was having from an old post
on these boards but no clear explanation was supplied.

Why does the label in the code below not reflect the value of the
TkVariable?

require 'tk'

class Tktest
   attr_writer :tkvar, :label1, :button1
   attr_accessor :tkvar, :label1, :button1

   def change
      @tkvar.value = "test!"
   end

  def initialize
     @tkvar = TkVariable.new
     root = TkRoot.new {title "This is a tk test"}

     label1 = TkLabel.new(root) do
        textvariable @tkvar
        pack
     end

     p = proc { change }
     button1 = TkButton.new(root) do
        text "Test it"
        command p
        pack
     end
  end
end

t = Tktest.new
Tk.mainloop

···

===========
When I press on the button, the label text is not altered. However, in
the change method, if I print the value of the TkVariable out to the
command line, it prints out the correct value.

Now, if I create a local varaible in the initialize method and assign
the TkVariable to it, then use the local variable for the textvariable
in the label, it works.

If someone could explain that, I would greatly appreciate it. Thanks,
eching.

Don't ask me why it is, but:

#label1 = TkLabel.new(root) do
  #textvariable @tkvar
  #pack
#end
label1 = TkLabel.new(root, :textvariable => @tkvar).pack

That works.

···

On 7/15/05, eching <bingopajama@hotmail.com> wrote:

Hi,

I am very green at Ruby and Ruby/Tk (just started working with it last
week, and love it so far)

I finally found a solution to the problem I was having from an old post
on these boards but no clear explanation was supplied.

Why does the label in the code below not reflect the value of the
TkVariable?

require 'tk'

class Tktest
   attr_writer :tkvar, :label1, :button1
   attr_accessor :tkvar, :label1, :button1

   def change
      @tkvar.value = "test!"
   end

  def initialize
     @tkvar = TkVariable.new
     root = TkRoot.new {title "This is a tk test"}

     label1 = TkLabel.new(root) do
        textvariable @tkvar
        pack
     end

     p = proc { change }
     button1 = TkButton.new(root) do
        text "Test it"
        command p
        pack
     end
  end
end

t = Tktest.new
Tk.mainloop

When I press on the button, the label text is not altered. However, in
the change method, if I print the value of the TkVariable out to the
command line, it prints out the correct value.

Now, if I create a local varaible in the initialize method and assign
the TkVariable to it, then use the local variable for the textvariable
in the label, it works.

If someone could explain that, I would greatly appreciate it. Thanks,
eching.

I like the looks of that better than using a local variable. Thanks!

Message-ID: <c715e6405071512504601135b@mail.gmail.com>

Don't ask me why it is, but:

#label1 = TkLabel.new(root) do
  #textvariable @tkvar
  #pack
#end
label1 = TkLabel.new(root, :textvariable => @tkvar).pack

That works.

The block given to 'new' method is passed to 'instance_eval' method.
That is, @tkvar in the block is not an instance variable of the Tktest
object but of the label widget. And then, because @tkvar is nil, the
textvariable option value of the widget is set to an empty string.

···

From: Joe Van Dyk <joevandyk@gmail.com>
Subject: Re: TkVarible question
Date: Sat, 16 Jul 2005 04:51:08 +0900
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Ah, I see... excellent. Thanks for the response.