[Tutor] How to redirect console output to a TextEdit box on a QT Python Gui ?

SM sunithanc at gmail.com
Wed Jun 19 22:03:32 CEST 2013


Hello Chris, Thanks for your response. I have a follow-up question, if you
don't mind, to understand your answer better.
I am running a python3 script. So  first part of your answer applies here:

*"If the application you run is a Python script, import it, execute the
functions and have the data returned (as opposed to printing it), and
then you can do self.textEdit.setText(output_*
*goes_here)"*

My python script prints a lot of text at various stages. There are "print"
statements spread across multiple python files.  So when you say "have the
data returned" and "output_goes_here" as the argument of setText, I am a
bit confused as to how I can redirect multiple print statements to the
setText call. Can you please clarify?

Thanks!
Sm




On Sun, Jun 16, 2013 at 2:02 PM, Chris “Kwpolska” Warrick <
kwpolska at gmail.com> wrote:

> On Sun, Jun 16, 2013 at 7:15 PM, SM <sunithanc at gmail.com> wrote:
> > Hi
> > I have implemented a GUI using PyQt4/python3, which allows the user to
> > select a few files as command-line arguments. When I hit "OK" button, my
> > application runs and the output text is displayed on the terminal where I
> > run the python script. I would like to redirect this text to a TextEdit
> > window I have created on the GUI:
> >
> > self.textEdit = QtGui.QTextEdit(Form)
> > self.textEdit.setGeometry(QtCore.QRect(10, 300, 421, 141))
> > self.textEdit.setObjectName(_fromUtf8("textEdit"))
> >
> > Do you have any suggestions/tips as to how I can accomplish this task?
> Using
> > some suggestions online,  I tried creating a QProcess object and connect
> the
> > signal to a function which  reads the std output. But I don't know how to
> > set the arguments to the call "process.start".
> >
> > self.process = QtCore.QProcess()
> > QtCore.QObject.connect(self.process, QtCore.SIGNAL("readyReadStdout()"),
> > self.readOutput)
> > I know I have to call self.process.start here but am not able to find
> > examples of how to do it. One example online uses
> > "self.process.setArguments", but python3/pyQt4 doesn't seem to know about
> > it. Using the argument directly with process.start is not helping either.
> >
> > Appreciate any pointers. If there are better ways than what I am trying
> to
> > use, appreciate that as well.
>
> If the application you run is a Python script, import it, execute the
> functions and have the data returned (as opposed to printing it), and
> then you can do self.textEdit.setText(output_goes_here)
>
> If the application isn’t Python, use the (stock aka vanilla)
> subprocess module.  The easiest solution is:
>
>     import subprocess
>     self.textEdit.setText(subprocess.check_output(('command',
> 'argument1', 'argument2')))
>
> And you do that in the function that is called when you press OK.
>
> Bonus suggestion: do nice layouts instead of setting the coordinates
> manually.  The options you want are in the Form menu of Qt Designer.
> If you don’t do this, resizing windows is nonsense.
>
> --
> Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
> stop html mail                | always bottom-post
> http://asciiribbon.org        | http://caliburn.nl/topposting.html
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130619/9e0a50e3/attachment-0001.html>


More information about the Tutor mailing list