[Tutor] How to redirect console output to a TextEdit box on a QT Python Gui ?
Chris “Kwpolska” Warrick
kwpolska at gmail.com
Sun Jun 16 20:02:29 CEST 2013
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
More information about the Tutor
mailing list