[Tutor] redirecting module output

Christian Meesters cmeesters at ucdavis.edu
Wed Aug 18 18:43:32 CEST 2004


	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



More information about the Tutor mailing list