[Tutor] redirecting module output

Kent Johnson kent_johnson at skillsoft.com
Wed Aug 18 22:23:17 CEST 2004


You might rewrite DNA to output to the Python logging system instead of 
stdout. Then you can configure the log to send the output where you want 
to. You could write a log handler that puts the output in your window. You 
could also use a handler that captures output to a file. I have used this 
approach in a Java program, it's very nice to have both the GUI display and 
the file.

You could use the wxWindows logging system, though this would make your DNA 
class depend on wxWindows which probably isn't such a good idea. You could 
use Alan's idea of passing in a log function to break that dependency.

Kent

At 09:43 AM 8/18/2004 -0700, Christian Meesters wrote:

>         From:     cmeesters at ucdavis.edu
>         Subject:        redirecting module output
>         Date:   August 18, 2004 8:06:22 AM PDT
>         To:       tutor at python.org
>
>Hi
>
>Reading this list is really great. Most contributions are 'just' nice and 
>helpful - in contrast to
>other forums ...
>
>That said, I'd like to address a questions for which I couldn't find a 
>solution, yet. My wish is to
>embed a program, which at first gave all its output on the shell (stdout), 
>into a GUI (using
>wxPython, BTW). Core of the original program was a module with a single 
>class (class and
>module called 'DNA'). The intention is partly to simplify my work, but 
>actually just to train GUI
>programming, which is new to me.
>
>Ok, before I come to the problem, first on how the head of the main script 
>(the GUI part) looks
>like:
>--------
>import sys
>import os
>import wx
># import own modules from the 'src' directory
>import src.DNA
>import src.NCBIstrip
>
>class FindFrame(wx.Frame,src.DNA.DNA):
>         def __init__(self,parent,id,title):
>                 wx.Frame.__init__(self,None,-1,"FindSequence")
>                 p = wx.Panel(self,-1)
>                 # code ... code ... code ...
>                 # and later a TxtCtrl-field is initialized:
>                 TextField = wx.TextCtrl(p,-1,"Nothing selected,
>yet.\n\n",size=(500,200),style=wx.TE_MULTILINE)
>                 self.TextField = TextField
>                 sizer.Add(TextField,(4,1),(1,3),flag=wx.EXPAND)
>                 # even more code ...
>
>--------
>
>With other words: Nothing but standard. (Or?)
>
>Now to the problem: Functions of the 'DNA' class (within the DNA module) 
>printed their output
>on the shell (stdout). Redirecting stdout to 'TextField' works, but causes 
>a delay: The output is
>not printed until the process producing it is finished. In the original 
>program I let functions
>within DNA print something on the shell to indicate that the process is 
>still running and which
>path of processing is followed. Output as text seems the most sensitive 
>way to accomplish both
>goals. But how is it possible to redirect the output from the imported 
>DNA-class to 'TextField'?
>Perhaps something with the conventional TextField.AppendText("anything") 
>altered? How do I
>have to alter print statements within the DNA class to accomplish this? Or 
>how could I use an
>indermediate buffer?
>
>Well, I hope I made myself clear. Describing this problem wasn't easy ...
>
>Thank you very much in advance,
>Christian
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list