Assorted Ruby/Tk Questions/Comments

I’m looking into using Ruby/Tk for a major project. Maybe I’m crazy,
but I find its interface cleaner than that of FOX. I’m very
experienced with Perl/Tk, but I’m just getting started with the Ruby
port. Here are some questions and observations:

  1. There appears to be a bug in TkRoot.new (or possibly TkWindow.new).
    On my Win2K box, the following code:

require ‘tk’

TkRoot.new do
puts "test"
end

produces:
test
test

The block is being called twice. This can be prevented with the
following code:

TkRoot.new do
return if @here
@here = true

...

end

This looks like a bug to me.

  1. Do I have to install Tcl on *NIX boxes to use Ruby/Tk? I know I
    can just redistribute the DLL’s on Windows, but how does this work on
    NIX? Does Ruby/Tk come with everything it needs to run right out of
    the box?

  2. How can I use TkTree? It looks like it’s in the tcltk-ext
    package, but I’m not sure how to use this on Windows. Or Linux for
    that matter. Does this also require Tcl to be installed?

  3. pack doesn’t accept a block, even though Programming Ruby implies
    that this is so. I’ve written the following code to allow pack to
    accept a block. It’s hackish, so maybe someone more familiar with the
    Ruby/Tk internals can improve it:

class TkWindow
class PackContext
attr_reader :args

	def initialize
		@args = {}
	end
	
	def method_missing(sym, *args)
		name = sym.id2name
		@args[name] = args[0]
	end
end

alias_method :old_pack, :pack

def pack(*args, &b)
	if block_given?
		pc = PackContext.new
		pc.instance_eval(&b)
		params = pc.args
	else
		params = *args
	end
	old_pack(params)
end

end

Then you can do things like

label.pack do
expand 1
fill "both"
end

which is kind of useful to me.

  1. Are there any special reasons not to use Tk? I need a toolkit
    with a clean interface and small footprint.

Thanks in advance for your help,
Bill Atkins

Hi,

···

From: dejaspam@batkins.com (Bill Atkins)
Subject: Assorted Ruby/Tk Questions/Comments
Date: Thu, 8 Apr 2004 05:24:18 +0900
Message-ID: e6056acc.0404071223.16e17aab@posting.google.com

The block is being called twice. This can be prevented with the
(snip)
This looks like a bug to me.

Yes. It’s a bug. Thank you for your report.
This problem will be fixed by the following patch.

— tk.rb.orig 2004-04-08 11:49:48.000000000 +0900
+++ tk.rb 2004-04-08 11:50:01.000000000 +0900
@@ -4532,7 +4532,7 @@
def TkRoot.new(keys=nil, &b)
unless TkCore::INTERP.tk_windows[‘.’]
TkCore::INTERP.tk_windows[‘.’] =

  •   super(:without_creating=>true, :widgetname=>'.')
    
  •   super(:without_creating=>true, :widgetname=>'.'){}
    
    end
    root = TkCore::INTERP.tk_windows[‘.’]
    if keys # wm commands
  1. Do I have to install Tcl on *NIX boxes to use Ruby/Tk? I know I
    can just redistribute the DLL’s on Windows, but how does this work on
    NIX? Does Ruby/Tk come with everything it needs to run right out of
    the box?

Yes. You must install Tcl/Tk libraries.
Ruby/Tk calls functions of Tcl/Tk library.

The Ancient Ruby/Tk used interp process communication
between Ruby and Wish. It’s too slow!!
But current Ruby/Tk loads Tcl/Tk libraries into Ruby.

It’s an important difference between Ruby/Tk and Perl/Tk.
Because of controlling pure Tcl/Tk libraries,
Ruby/Tk can use almost all of Tcl/Tk extentions.
Of course, before using a extention from Ruby,
you must make your Tcl/Tk can use the extention.

However, to control Tcl/Tk extentions conveniently,
you will define classes and methods for the extentions.
The ‘tcltk-ext’ library creates the base of definition automatically.

  1. How can I use TkTree? It looks like it’s in the tcltk-ext
    package, but I’m not sure how to use this on Windows. Or Linux for
    that matter. Does this also require Tcl to be installed?

Do you say about TixTree?
If you want to use TixTree class, your Tcl/Tk must be able to use
Tix extention.

  1. pack doesn’t accept a block, even though Programming Ruby implies
    that this is so. I’ve written the following code to allow pack to
    accept a block. It’s hackish, so maybe someone more familiar with the
    Ruby/Tk internals can improve it:
    (snip)
    Then you can do things like

label.pack do
expand 1
fill “both”
end

which is kind of useful to me.

Hmmm… Do you want the feature strongly?
Cannot a Hash parameter satisfy you?
(e.g label.pack(:expand=>true, :fill=>‘both’) )

                              Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)