[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

TkVarible question

eching

7/15/2005 6:38:00 PM

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.

3 Answers

Joe Van Dyk

7/15/2005 7:51:00 PM

0

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.


eching

7/15/2005 8:01:00 PM

0

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

Hidetoshi NAGAI

7/17/2005 11:01:00 PM

0