Learning ruby and tk

Hi all,

I am getting inside tcltk.rb. I have a question with callbacks: how can
I pass parameters to a non-bind callback? E.g., I have a proc which can
handle many button’s click if I could pass a parameter that which button
was pressed (for example). And I do not want to write as many procs as
many buttons I have. It would be the best if I do not need to write as
many callbacks either, i.e., I could pass the parameters where I create
the button widget.

Unfortunately, I cannot pass parameters to the callback, as (because of
bind) the ‘%s’ in the 1st arg of ruby_fmt is replaced with %%s. And the
(proper) ‘%s’ is put only when there are no parameters (I do not
understand why).

The relevant code from tcltk.rb (class TclTkCallback):

def to_eval()
if @arg
# bind replaces %s before calling ruby_fmt, so %%s is used
s = %Q/{ruby_fmt {TclTk._callcallback("#{to_s()}", “%%s”)}
#{@arg}}/
else
s = %Q/{ruby_fmt {TclTk._callcallback("#{to_s()}", “%s”)}}/
end

return s

end

Is it impossible what I want to do, or do I miss something?

Thanks:
Circum

Hi,

···

From: Ferenc Engard engard@all.hu
Subject: learning ruby and tk
Date: Tue, 7 Jan 2003 08:22:43 +0900
Message-ID: 3E1A0E91.E0061528@all.hu

I am getting inside tcltk.rb. I have a question with callbacks: how can
I pass parameters to a non-bind callback? E.g., I have a proc which can
handle many button’s click if I could pass a parameter that which button
was pressed (for example). And I do not want to write as many procs as
many buttons I have. It would be the best if I do not need to write as
many callbacks either, i.e., I could pass the parameters where I create
the button widget.

Probably, nobody maintains tcltk.rb. Why don’t you use tk.rb?
Isn’t the following script enough for you?

#!/usr/bin/env ruby
require ‘tk’

param = ''
cmd = proc{|arg| p arg; param = arg}

TkButton.new(nil, ‘text’=>‘quit’, ‘command’=>proc{exit}).pack(‘fill’=>‘x’)
TkButton.new(nil, ‘text’=>‘print’, ‘command’=>proc{p param}).pack(‘fill’=>‘x’)

TkButton.new(nil, ‘text’=>‘foo’,
‘command’=>proc{cmd.call([0,‘foo’])}).pack(‘fill’=>‘x’)

[‘bar’, ‘baz’, ‘hoge’, ‘huga’].each_with_index{|n,i|
TkButton.new(nil, ‘text’=>n){
bind(‘ButtonRelease-1’, cmd, “%W (%x,%y) #{i+1} #{n}”)
pack(‘fill’=>‘x’)
}
}

Tk.mainloop


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

Probably, nobody maintains tcltk.rb. Why don’t you use tk.rb?
Isn’t the following script enough for you?

It seems right… I thought that tk.rb is based on tcltk.rb, now I have
realized that this is not true. So I start to understand tk.rb… Maybe,
if it will be clear to me, I put together a tutorial in english. This
painful lack of english documentation… :(((

Bye:
Circum