Similar Threads:
1.Ruby Tk and X/Y coordinates
2.Tk - Ruby/Tk core library design questions
I forgot which old version is, on that old version, the widget.pack
method always returns nil.
So people have to write something like:
b = TkButton.new {
pack
}
Now the pack returns self, what's great improvement, so we can write
something like:
b = TkButton.new.pack.command { puts "Hello" }
Now, my ruby version is : ruby 1.8.2 (2004-12-25) [i386-mswin32]
and my Tcl/Tk version is : 8.4
I have some Ruby/Tk design questions :
(1) Layout Manager:
there are:
widget.pack ; TkPack.pack ; Tk.pack
widget.grid ; TkGrid.grid ; Tk.grid
widget.place ; TkPlace.place
methods, why there is no Tk.place method ?
This seems inconsistent for me.
(2) Since we try to wrap Tcl/Tk on OO, why some Tcl commands are not
treat like options ?
Most options have getter and setter method, there are many way to do
it, for example:
# setter
button.bg = :red
button.bg(:red)
button.configure(:bg, :red)
# getter
puts button.bg
puts button.cget(:bg)
Now, for example, root.title and root.iconbitmap ... etc are command
but not options on Tcl/Tk language.
But, I feel this will be nice if we "wrap" them like options, so we
can write something like:
TkRoot.new(:title=>'Hello') or
TkButton.new(:text=>'set title').command { Tk.root.title = "some title" }
For me, it seems title is just like an option for TkRoot.
(But in current version, it is not, so I cannot use :title=>... or
root.title = ... syntax )
(3)
require 'tk'
c = TkCanvas.new.pack
TkcRectangle.new(c, 10, 10, 50, 50)
Tk.mainloop
The above code works fine, but if I rewrite it to become
require 'tk'
TkcRectangle.new(TkCanvas.new.pack, 10, 10, 50, 50)
Tk.mainloop
I got this error: uninitialized constant TkcRectangle (NameError)
Rewrite it again like:
require 'tk'
TkCanvas
TkcRectangle.new(TkCanvas.new.pack, 10, 10, 50, 50)
Tk.mainloop
It works fine again.
So, what's going wrong with:
require 'tk' ; TkcRectangle.new(TkCanvas.new.pack, 10, 10, 50, 50) ; Tk.mainloop
Why should I reference TkCanvas ( at least class ) first ?
Thank you.
3.ruby graph: coordinate plane?
4.Noob Q: ruby block scoping question (ruby TK)
I've just recently been getting to know ruby and the ruby Tk library and
have run into a block scoping problem [apparently] that I don't really
understand (I'm a long-time C programmer, mostly at the systems and
library levels, and have little experience with VHLLs such as ruby).
I've included below a cobbled up example that demonstrates the problem
that has been driving me completely batty.
Basically, why is the "mybutton" method reference not contained within
the binding of the block passed to the TkButton.new method though it is
clearly within the scope of the initialize_n methods?
I guess my question boils down to is how much of the block's context is
actually contained within its binding, i.e., how far out does it extend?
Obviously, it extends at least to that of the method within which it
appears. Why not to all symbols visible within the class definition
(as, apparently, it doesn't)?
I do realize that the actual block passed to the "command" method in the
outer block passed to the TkButton.new method is actually executed in
the context of the button code itself somewhere in the Tk library and,
therefore, executes in -that- context. However, it's binding should
carry over some of the context in which it was created. How much (i.e.,
to what extent)?
Any references to books, online docs, code examples, ruby interpreter
sources, etc., in this regard would also be much appreciated.
Additionally, any insights as to what goes on "behind the scenes" in
ruby would be very helpful.
Thanks!
Phil
----- Begin ruby code -----
#!/usr/bin/ruby -w
require 'tk'
class MyButton
def myexit
exit
end
# Doesn't work: "myexit" out of scope in block. Why?
def initialize_1
mybutton = TkButton.new do
text "EXIT"
command { myexit }
pack
end
end
# Works: refs "myexit" outside of block.
# Note that "myexit" -is- visible within the initialize_2 method!?!
def initialize_2
mybutton = TkButton.new do
text "EXIT"
pack
end
mybutton.command { myexit }
end
# Works: creates local proc object to pass to block.
# Note that "myexit" -is- visible within the initialize_3 method!?!
def initialize_3
proc_myexit = lambda { myexit }
mybutton = TkButton.new do
text "EXIT"
command proc_myexit
pack
end
end
# Pick one of "initialize_[123]" to perform the test cases.
def initialize
initialize_1
#initialize_2
#initialize_3
Tk.mainloop
end
end
MyButton.new
----- End Ruby Code -----
--
Philip Amadeo Saeli
SUSE Linux 10.1
XXXX@XXXXX.COM
5.2 questions about TkVariable and ruby/tk
Hi all!
I have the following code snippet:
-----------------------------------
...
h=Hash.new
(0..4).each {|c|
(0..9).each {|r|
h["#{r},#{c}"]="#{r} ; #{c}";
}
}
v=TkVariable.new(h)
#~ puts "before: #{v.value}"
puts "ok."
t2=TkTable.new() {
rows 10
cols 5
variable v
}
t2.pack()
Tk.mainloop()
-------------------------------------
(TkTable is a wrapper for a tktable tcl widget written by me. It uses
'variable' to store the data of the table cells.)
This works perfectly, just as I expect. BUT, if I comment out the
debugging puts(), then it prints v's content, then 'ok.' just as before,
but after that the program hangs, the X window do not appear, and I
cannot even stop the program with CTRL-C! If I simply just read the
v.value (do not print it), the result is similar. :-O With v.inspect the
problem do not occur. What difference makes reading a TkVariable's
value???
My second question is related to the previous: I have the feeling that
this 'hang' really means a runtime error in the ruby code which is
executed from tcl (which is executed from ruby :). So, my question is
how to receive these error messages, and why do the process hang if it
encounters with an error like this?
6. Ruby-tk question
7. ruby/tk question
8. Ruby TK window size question