continuous redirection of stdout & stderr to Tkinter Text

Fabien Hénon ffjhenon at club-internet.fr
Wed Jun 26 17:31:28 EDT 2002


I am using Tkinter as an editor and a launcher (under Linux) for a
raytracer (POV-RAY)

Once started, the raytracer does two things in the same time :
- it displays the image being raytraced
- it spits out information to the console (ie : the line which is being
raytraced). 
I get the image displayed but not the redirection of stdout

That is why I use the root.after function to (try to) update every 10th
second.


With the script below, I get the image displayed, but not the stdout (or
stderr).

What am I doing wrong ?

To the pipe-redirectors gurus : Help !

8<---------snip------------------------->8
from Tkinter import *
import sys, os
import popen2

root = Tk()

class StringVarFile:
    def __init__(self,stringVar,window):
        self.__newline = 0
        self.__stringvar = stringVar
        self.__window = window

    def write(self,s):
        new = self.__stringvar.get()
        for c in s:
            if c == '\n':
                self.__newline = 1
            else:
                if self.__newline:
                    new = ""
                    self.__newline = 0
                new = new+c
        self.set(new)

    def set(self,s):
        self.__stringvar.set(s)
        self.__window.update()

    def get(self):
        return self.__stringvar.get()


class mainWin:
    def __init__(self,tkRoot):
        self.tkRoot=tkRoot
        self.createWidgets()
        return None

def runpov():
    cmd='megapovplus +i1.pov +w320 +h240 +dgt +v -f +p'
    run=popen2.Popen3(cmd)
    upd()


def upd():
       resultat.insert('1.0',statusVar))
       root.after(100,upd())




mframe=Frame(root)

bouton=Button(mframe,text="Feu !", command=runpov)
bouton.pack()
statusVar = StringVar()
#resultat=Label(mframe, width=60, height=10,justify=LEFT, anchor=W,
textvariable=statusVar)
resultat=Text(mframe)
resultat.pack(fill=X,expand=Y)
mframe.pack(expand=1, fill=BOTH)

status = StringVarFile(statusVar,root)
sys.stderr=status

root.mainloop()
8<---------snap------------------------->8




More information about the Python-list mailing list