Printing to a "log screen" in a multi-module QT app
Bjorn Pettersen
BPettersen at NAREX.com
Wed Feb 27 16:00:11 EST 2002
> From: Anders Schneiderman [mailto:schneida at seiu.org]
>
> I'm writing an application using QT. Right now when I want
> to use the equivalent of "print", I call a method called
> "log" that's defined in the main class in my main module:
>
> def log(self, msg):
> try:
> self.logArea.insertline(msg, -1)
> except:
> self.logArea.insertline(str(msg), -1)
>
> To call it in other classes that are in other modules of the
> app, I pass "log" to the new class. For ex:
>
> class CodeEditor:
> def __init__ (self, log, ...)
> self.log = log
>
> Is there a cleaner way to do this? For example, is there a
> way to redefine the "print" command? Whatever solution I
> come up with, the command to print has to be short;
> "self.logObject.log('File successfully updated')" is too much
> of a pain. Any suggestions?
You could do something like:
class Logger:
def __init__(self, logArea):
self.logArea = logArea
def write(self, msg):
try:
self.logArea.insertline(msg, -1)
except:
self.logArea.insertline(str(msg), -1)
Then you can pass an instance of the Logger class around and simply do:
print >> logger, 'hello world'
-- bjorn
More information about the Python-list
mailing list