Tk app, reading output from command and put it in a text widget

subject says it all.

I have a button which launches and externcal command. I want to capture the output of that command and append it to my text area. Can I do this with events?

Thanks,

~Shea M.

Message-ID: <XIW0f.104652$Ph4.3184945@ursa-nb00s0.nbnet.nb.ca>

I have a button which launches and externcal command. I want to capture
the output of that command and append it to my text area. Can I do this
with events?

Hmmm...
Maybe, TkTextIO class (a Tk sample included in Ruby-1.8.3) is useful.
For example,

···

From: Shea Martin <null@void.0>
Subject: tk app, reading output from command and put it in a text widget
Date: Thu, 6 Oct 2005 05:31:49 +0900
-----------------------------------------------------------------------
require 'tk'
require 'open3'

# use TkTextIO class which is one of the sample scripts on Ruby source archive.
tk_sample_dir = '/usr/local/src/ruby-1.8.3/ext/tk/sample'
require File.join(tk_sample_dir, 'tktextio.rb')

f = TkFrame.new.pack
tio = TkTextIO.new(f, :show=>:pos){
  yscrollbar(TkScrollbar.new(f).pack(:side=>:right, :fill=>:y))
  pack(:side=>:left, :fill=>:both, :expand=>true)
}

org_stdout = $stdout
$stdout = tio
$stderr = tio

# execute an external command
# ( In this way, cannot determine the output order between stdout and stderr. )
def ex_cmd(cmd)
  p_in, p_out, p_err = *Open3.popen3(cmd)

  [
    p_in,
    Thread.new{ p_out.each{|line| print(line) } },
    Thread.new{ p_err.each{|line| print(line) } }
  ]
end

f = TkFrame.new.pack(:fill=>:x, :padx=>40)

cmd = '/bin/cat - nofile' # external command

TkButton.new(f, :text=>'exec external command'){|b|
  command {
    b.state(:disabled) # disable the button while ex_cmd is running
    Thread.new{ # create a thread to avoid hang-up of the eventloop
      begin
        p_in, out_th, err_th = ex_cmd(cmd)
        p_in.print("foo foo foo\n")
        p_in.print("bar bar bar\n")
        p_in.print("baz baz baz\n")
        p_in.print("hoge hoge hogw\n")
        p_in.print("fuga fuga fuga\n")
        p_in.close_write
        out_th.join
        err_th.join
      ensure
        b.state(:normal) # enable the button
      end
    }
  }
}.pack(:side=>:left)

# TkTextIO is a subclass of TkText
TkButton.new(f, :text=>'print log to console',
             :command=>proc{org_stdout.print(tio.value)}).pack(:side=>:right)

Tk.mainloop
-----------------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Shea Martin wrote:

subject says it all.

I have a button which launches and externcal command. I want to capture the output of that command and append it to my text area. Can I do this with events?

Thanks,

~Shea M.

I tried fork, but isn't supported in win32....

~S

Hidetoshi NAGAI wrote:

Hmmm... Maybe, TkTextIO class (a Tk sample included in Ruby-1.8.3) is useful.

Ruby 1.8.2 Win32 does not seem thave TkTextIO. A find in my Ruby tree did not turn up the work TkTextIO in any file in my ruby install.

Does 1.8.3 have TkTextIo for win32?

Thanks,

~Shea M.

Message-ID: <A%91f.104986$Ph4.3195085@ursa-nb00s0.nbnet.nb.ca>

Ruby 1.8.2 Win32 does not seem thave TkTextIO. A find in my Ruby tree
did not turn up the work TkTextIO in any file in my ruby install.

"tktextio.rb" is a pure Ruby/Tk script.
It means that TkTextIO doesn't need any DLL,
and works if Ruby/Tk works.

The file is one of the sample scripts.
So, even if you install Ruby-1.8.3 by "make install",
it will not be installed.

Please get the file from the Ruby-1.8.3 source archive or CVS.
Probably, it will work on Ruby-1.8.2.

···

From: Shea Martin <null@void.0>
Subject: Re: tk app, reading output from command and put it in a text widget
Date: Thu, 6 Oct 2005 22:56:49 +0900
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)