Class definition example question

Greg McFarlane gregm at iname.com
Thu Dec 9 07:23:42 EST 1999


I think you could turn stdoutHandler() into a normal (private) method
and then pass it into createfilehandler, like this:

class execWindow(Pmw.ScrolledText):

    def run(self,progPath):
	...
	Tkinter.tkinter.createfilehandler(self.__child.fromchild,
				  Tkinter.tkinter.READABLE,
				  self._stdoutHandler)

    def _stdoutHandler(self, file, mask):
	...
	
Or am I missing something?

On 7 Dec, Grant Edwards wrote:
> [background]
> 
> I'm working on a program to do design verification and
> production test on a circuit board.  Most of the actual testing
> is done by a C program with a whole boatload of obscure command
> line options.  It works but it's not very friendly, so I'm
> going to slap a GUI front-end on it.  I generally use STk for
> stuff like this, but this time I decided to try Python since
> most of the Linux machines around here come with Python and
> Tkintr already installed, and I've got a better chance of
> dumping maintenance duties on somebody else if it's not in
> Scheme. ;)
> 
> [my example]
> 
> I've extended the Pmw "ScrolledText" class to add a "run"
> method that runs a program in a sub-process and displays the
> standard output in the contained text widget.  The class
> definition is shown below.
> 
> My question is about the definition of the callback handler
> "stdoutHandler".  If it's done this way, you end up with a
> complete copy of the handler routine defined each time a
> program is run.  I couldn't figure out any other way for the
> callback routine to know what widget it belonged to.
> 
> It seems to work, but I'm pretty sure there's a more elegant
> way to do this.
> 
> Would it be better to create a callback class with an instance
> variable that points to associated widget?  Can an object
> instance be passed to tkintr.createfilehandler?  Is this where
> one needs to define a __call__ method?
> 
> Any helpful comments will be appreciated
> 
> #----------------------------------------------------------------------
> 
> class execWindow(Pmw.ScrolledText):
> 
>     def run(self,progPath):
>     
>         def stdoutHandler(file,mask,s=self):
>         
>             # read available data
>             line = os.read(s.__fd,4096)
>             
>             # display it in window
>             
>             if line != "":
>                 s.insert('end',line)
>             
>             # done?
>             
>             r = s.__child.poll()
>             
>             if r != -1:
>                 Tkinter.tkinter.deletefilehandler(file)
>                 s.__returnCode = r
>                 s.insert('end', '[return=' + str(r) +']\n')
>             
>         self.__child = popen2.Popen3(progPath)
>         self.__fd = self.__child.fromchild.fileno()
>         
>         fcntl.fcntl(self.__fd, FCNTL.F_SETFD, FCNTL.O_NDELAY);
>         
>         Tkinter.tkinter.createfilehandler(self.__child.fromchild,
>                                   Tkinter.tkinter.READABLE,
>                                   stdoutHandler)
>                                   
> #----------------------------------------------------------------------
> 
> -- 
> Grant Edwards                   grante             Yow!  I guess we can live
>                                   at               on his POT FARM in HADES!!
>                                visi.com            
> -- 
> http://www.python.org/mailman/listinfo/python-list
> 

-- 
Greg McFarlane     INMS Telstra Australia     gregm at iname.com




More information about the Python-list mailing list