Home > Archive > Ruby > August 2005 > Tk Label and instance variables
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Tk Label and instance variables
|
|
| Bart Masschelein 2005-08-30, 7:02 pm |
| Hello all,
Below is a small piece of test code for a label using Tk. I try to set a
label using an instance variable, and one using a local variable. The
label with the local variable shows correctly in the window, the one
with the instance variable doesn't. I get a warning: instance variable
@value not initialized. Although the puts in between proves differently.
What is the magic between all of this?
I tried adding value to attr_reader, but that didn't solve anything. Not
that I was expecting it to.
As you probably can see, I come from a C/C++ background, and am probably
missing some Ruby fundamentals to solve this. My collection of example
scripts is rather small for the moment, and hence is also my experience.
So any directions are welcome.
gr.b.
class LabelTest
def initialize(value)
@value = value
end
def run
root = TkRoot.new { title "Label test" }
puts @value
TkLabel.new(root) do
text "#{@value}"
pack { padx 15 ; pady 15; side 'left' }
end
value2 = 200
TkLabel.new(root) do
text "#{value2}"
pack { padx 15 ; pady 15; side 'left' }
end
Tk.mainloop
end
end
test = LabelTest.new(100)
test.run
| |
| Bart Masschelein 2005-08-30, 7:02 pm |
| Oh, and of course I could get around with the following, but I would
like to know why the instance variable appears to be not initialized
inside the TkLabel initialization.
value3 = @value
TkLabel.new(root) do
text "#{value3}"
pack { padx 15 ; pady 15; side 'left' }
end
Bart Masschelein wrote:
> Hello all,
>
> Below is a small piece of test code for a label using Tk. I try to set
> a label using an instance variable, and one using a local variable.
> The label with the local variable shows correctly in the window, the
> one with the instance variable doesn't. I get a warning: instance
> variable @value not initialized. Although the puts in between proves
> differently. What is the magic between all of this?
>
> I tried adding value to attr_reader, but that didn't solve anything.
> Not that I was expecting it to.
>
> As you probably can see, I come from a C/C++ background, and am
> probably missing some Ruby fundamentals to solve this. My collection
> of example scripts is rather small for the moment, and hence is also
> my experience. So any directions are welcome.
>
> gr.b.
>
>
> class LabelTest
> def initialize(value)
> @value = value
> end
>
> def run
> root = TkRoot.new { title "Label test" }
> puts @value
> TkLabel.new(root) do
> text "#{@value}"
> pack { padx 15 ; pady 15; side 'left' }
> end
> value2 = 200
> TkLabel.new(root) do
> text "#{value2}"
> pack { padx 15 ; pady 15; side 'left' }
> end Tk.mainloop
> end
> end
>
> test = LabelTest.new(100)
> test.run
>
>
>
>
| |
| Hidetoshi NAGAI 2005-08-30, 9:57 pm |
| From: Bart Masschelein <bart.masschelein@skynet.be>
Subject: Re: Tk Label and instance variables
Date: Wed, 31 Aug 2005 03:53:02 +0900
Message-ID: <4314AAFD.3040900@skynet.be>
> Oh, and of course I could get around with the following, but I would
> like to know why the instance variable appears to be not initialized
> inside the TkLabel initialization.
Maybe, that is a FAQ. :-)
The block given to 'new' method is evaluated by 'instance_eval'.
That is, in the block, self is the created widget object.
So, you can abbreviate the receiver widget object to configure
widget options or to call instance methods of the widget objcet.
In your test script, @value in the block is not an instance variable
of the LabelTest object but an uninitialized instance variable of
the widget object.
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
|
|
|
|
|