TkVarible question

E

eching

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.
 
J

Joe Van Dyk

Hi,
=20
I am very green at Ruby and Ruby/Tk (just started working with it last
week, and love it so far)
=20
I finally found a solution to the problem I was having from an old post
on these boards but no clear explanation was supplied.
=20
Why does the label in the code below not reflect the value of the
TkVariable?
=20
require 'tk'
=20
class Tktest
attr_writer :tkvar, :label1, :button1
attr_accessor :tkvar, :label1, :button1
=20
def change
@tkvar.value =3D "test!"
end
=20
def initialize
@tkvar =3D TkVariable.new
root =3D TkRoot.new {title "This is a tk test"}
=20
label1 =3D TkLabel.new(root) do
textvariable @tkvar
pack
end
=20
p =3D proc { change }
button1 =3D TkButton.new(root) do
text "Test it"
command p
pack
end
end
end
=20
t =3D Tktest.new
Tk.mainloop
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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.
=20
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.
=20
If someone could explain that, I would greatly appreciate it. Thanks,
eching.

Don't ask me why it is, but:

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

That works.
 
H

Hidetoshi NAGAI

From: Joe Van Dyk <[email protected]>
Subject: Re: TkVarible question
Date: Sat, 16 Jul 2005 04:51:08 +0900
Message-ID: said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top