Joe Van Dyk
7/15/2005 7:51:00 PM
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.
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.