Joshua Strot
Joshua Strot

Reputation: 2521

How to make a editable widget that updates in live time in PyQT

I'm trying to make a GUI that updates my system in PyQT4. I would to make it so that it ran all the commands in live time in the GUI, so that you could watch it update. I'm unsure what type of widget I would use to do that.

An example of this would be like how wget when ran has the status bar of the download, put the output of that into a widget.

I would imagine that you would use the subprocess library to run the command, and then somehow direct the output to the contents of the widget, but I'm completely unsure how to do this.

Any help is much appreciated.

Upvotes: 0

Views: 492

Answers (1)

László Papp
László Papp

Reputation: 53173

You could use a simple QLabel instance for this task, I believe. However, if you would need more fancy visualization, you could also go for a read-only QTextEdit, et al.

As for the processing code, you would write something like the code below if you happen to choose QProcess rather than the subprocess module in python.

from PyQt4.QtCore import QTimer, pyqtSignal, QProcess, pyqtSlot from PyQt4.QtGui import QLabel

class SystemUpdate(QProcess)
    """
    A class used for handling the system update process
    """

    def __init__(self):
        _timer = QTimer()
        _myDisplayWidget = QLabel()

        self.buttonPressed.connect(self.handleReadyRead)
        self.error.connect(self.handleError)
        _timer.timeout.connect(self.handleTimeout)

        _timer.start(5000)

    @pyqtSlot()    
    def handleReadyRead(self):
        _readData = readAll()
        _myDisplayWidget.append(readData)

        if not _timer.isActive():
            _timer.start(5000)

    @pyqtSlot()
    def handleTimeout(self):
        if not _readData:
            _myDisplayWidget.append('No data was currently available for reading from the system update')
        else:
            _myDisplayWidget.append('Update successfully run')

    @pyqtSlot(QProcess.ProcessError)
    def handleError(self, processError)
        if processError == QProcess.ReadError:
            _myDisplayWidget.append('An I/O error occurred while reading the data, error: %s' % _process->errorString())

Note: I know there is no append method for the QLabel class, but you could easily write such a convenience wrapper with the help of the available methods.

As for completeness, here goes the python subprocess approach:

import subprocess
from PyQt4.QtGui import QLabel

output = ""
try:
    """
        Here you may need to pass the absolute path to the command
        if that is not available in your PATH, although it should!
    """
    output = subprocess.check_output(['command', 'arg1', 'arg2'], stderr=subprocess.STDOUT)
exception subprocess.CalledProcessError as e:
    output = e.output
finally:
    myDisplayWidget.append(output)

Upvotes: 1

Related Questions