Re: Putting output of a running command into a text widget on the fly
- From: Andrew Mangogna <amangogna@xxxxxxxxxxxxxx>
- Date: Tue, 01 Apr 2008 18:09:44 -0700
Pavel Novozhilov wrote:
Hello,
Could anyone point me into the right direction? I have created a GUI
tool to download backups from remote UNIX systems over FTP. The tool
consists of two parts - GUI script itself and a download script. GUI
scrips is just a bunch of check buttons for every system, "Download"
button and text widget where some log information is displayed. The
download script is a CLI script and it is also used without GUI by
other scripts. GUI script invokes download script when "Download"
button is clicked with hostname parameter, pretty simple. While it
runs the download scripts produces some output like what file is being
downloaded from what remote system etc. My difficulty is that I'm
cannot figure out how to capture the output of the download script and
insert it into the text widget while the download is still running so
it would show some progress.
Right now when I click "Download" button the GUI freezes until
download is completed and I have no visibility to what's going on.
Could someone tell me what is the area of Tk that I should be studying
to achieve this behavio?
Thanks,
Pavel.
The usual approach to this situation is to use event driven I/O. In general
terms it goes something like this:
1. Run the command at the end of a pipeline that you are going to read.
The "open" command can do this for you when it is used in its
open "| cmd ..." form.
2. Set the channel returned from "open" to be non-blocking using
the "fconfigure" command (or "chan configure" for 8.5).
3. Set up an event handler to run when the channel becomes readable. This is
accomplished with the "fileevent" command (or "chan event" in 8.5).
4. In the event handler, the channel is read and the text can be inserted
into a text widget using something like ".t insert end ...". The event
handler does need to detect EOF and close the channel when that occurs.
This way of doing the I/O allows the GUI event loop to continue to run while
waiting for the asynchronous output from the other process. When the output
from the other process arrives, the channel becomes readable and the event
handler is dispatched. This is very analogous to what happens when commands
are executed by pressing GUI buttons. It is just in this case you have
arranged for the I/O to come from some place other than the mouse.
The wiki is always a good source for specific code sequences of this sort.
--
Andrew Mangogna
.
- Follow-Ups:
- Re: Putting output of a running command into a text widget on the fly
- From: Pavel Novozhilov
- Re: Putting output of a running command into a text widget on the fly
- References:
- Putting output of a running command into a text widget on the fly
- From: Pavel Novozhilov
- Putting output of a running command into a text widget on the fly
- Prev by Date: Re: Tcl/Tk 8.5.2 RELEASED
- Next by Date: Re: HTTP 1.1
- Previous by thread: Putting output of a running command into a text widget on the fly
- Next by thread: Re: Putting output of a running command into a text widget on the fly
- Index(es):