You appear to be learning by reading the docs for each part, then seeing
how to put them together one by one.
Learn by downloading the ruby tk examples; e-search for
“ruby-tk81-demos-english”, then take one of the (overgrown) examples and
(painstakingly) remove each bit you don’t need. A vaguely OO model will pop
out.
I have looked for ruby-tk81-demos-english (and other) examples, but
didn’t find what I am looking for. I am trying to learn from the source,
but there are things what I cannot figure out.
For example, the following example:
#!/usr/bin/ruby
require “tk”
ctx=binding()
Tk::INTERP._eval(“set a blahblah”)
eval ‘Tk::INTERP._eval(“puts $a”)’,ctx
Returns “blahblah”, so it works as I expect.
With the following, I have problems:
···
#!/usr/bin/ruby
require “tk”
$topContext=binding()
evalFrame=TkFrame.new { pack }
evalBox=TkText.new(evalFrame) {
height 10
pack(‘fill’=>‘x’)
}
evalBtn=TkButton.new(evalFrame) {
text “Execute”
command proc {evalText(evalBox.value)}
pack()
}
c=TkCanvas.new {
background “white”
pack(“fill”=>“both”)
}
def evalText(text)
begin
puts eval text,$topContext
rescue Exception => e
puts “Error running script: #{e}”
end
end
Tk.mainloop
This is a test script, for executing ruby code runtime in the top
context, as I have mentioned in my previous post. Try it out, if it is
not clear!
If I evaluate the following text with it:
Tk::INTERP._eval(“set a blahblah”)
Tk::INTERP._eval(“puts $a”)
It returns “blahblah” → Ok.
BUT! If I evaluate the two lines step by step (i.e. with two
button-clicks), then I receive this:
blahblah ← after 1st click, the value of first eval()
Error running script: c:/ruby/lib/ruby/1.6/tk.rb:971:in `_eval’: can’t
read “a”: no such variable ← after 2nd click, a is not defined! 
Also, if I execute the string
Tk::INTERP._eval(“puts [info vars]”)
It results only one variable, “args”, with the value “c0000”.
I know the solution is somewhere with the tk package’s callback
mechanism, but I cannot figure it out. Could somebody describe it,
please?
Thanks:
Circum