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

S

Shea Martin

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.
 
H

Hidetoshi NAGAI

From: Shea Martin <[email protected]>
Subject: tk app, reading output from command and put it in a text widget
Date: Thu, 6 Oct 2005 05:31:49 +0900
Message-ID: said:
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,
-----------------------------------------------------------------------
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
 
S

Shea Martin

Shea said:
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
 
S

Shea Martin

Hidetoshi said:
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.
 
H

Hidetoshi NAGAI

From: Shea Martin <[email protected]>
Subject: Re: tk app, reading output from command and put it in a text widget
Date: Thu, 6 Oct 2005 22:56:49 +0900
Message-ID: said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top