TKinter + display of a shell command return

klappnase klappnase at web.de
Thu Sep 9 15:39:47 EDT 2004


"Yann.K" <yk.suse at bullier.org> wrote in message news:<chnlg9$199t$1 at biggoron.nerim.net>...
> 
> Yes, really it run great but no as i would!
> For long process, the display wait the end of the script execution to
> display all the lines of the mesage.
> 
> I would that the lines appears as soon as the shell putt the message.
> So; if the treatment is very long (ie 10 min), the line of the message
> appears every second (in fact h*just when they are forwarder from the
> shell.
> 
> I would display the shell return like an "tail -f syslog" command on
> linux...
> I hope to be clearer...
> 
> Thanks for your help,

You can use a tk filehandler to capture the output stream of a shell
command.
Here's a code snippet I used to display the output of a shell command
in a text widget:

from Tkinter import *
import fcntl, popen2, os

    (...)
    self.text.insert('end', '\nExecuting :\n' + cmd + '\n\n')
    self.pp = popen2.Popen4(cmd)
    # cmd is of course the shell command
    self.mkfilehandler(self.pp, self.get_msg)

    def mkfilehandler(self, popen4object, function):
        fileobject = popen4object.fromchild
        filedescr = fileobject.fileno()
        fcntl.fcntl(filedescr, fcntl.F_SETFL, os.O_NONBLOCK)
        tkinter.createfilehandler(fileobject, READABLE, function)

    def get_msg(self, fileobject, mask):
        msg = self.pp.fromchild.read()
        if msg == '':
            p = self.pp.poll()
            if p != -1:
                tkinter.deletefilehandler(self.pp.fromchild)
                self.pp = None
                if p == 0:
                    tkMessageBox.showinfo(...)
                else:
                    tkMessageBox.showerror(...)
        else:
            self.text.insert('end', msg)

This works well at least on linux, on windows the createfilehandler()
method may not be available:

http://mail.python.org/pipermail/python-list/2002-February/089526.html

More information on createfilehandler() can be found at:

http://www.python.org/doc/faq/gui.html#id14

I hope this helps

Michael



More information about the Python-list mailing list