Skip to content Skip to sidebar Skip to footer

Display Installation Progress In Pyqt

I've written a little scrip to run a command for installing software on ubuntu.Here it is: from PyQt4 import QtCore, QtGui from subprocess import Popen,PIPE try:

Solution 1:

A simple way is to run a timer, poll the process's stdout periodically, and update the progress bar accordingly.

classUi_MainWindow(object):
    _timer = None# ...defruncmnd(self):
        self.p = Popen #...skipped. Note that p is now a member variable
        self._timer= QTimer(self)
        self._timer.setSingleShot(False)
        self._timer.timeout.connect(self.pollProgress)
        self._timer.start(1000)    # Poll every second; adjust as neededdefpollProgress(self):
        output = self.p.stdout.read()
        progress = # ...Parse the output and update the progress barif progress == 100:  # Finished
            self._timer.stop()
            self._timer = None

Some error-checking will be needed (when the network is faulty, user enters wrong password, etc.), of course.

By the way, Popen('sudo apt-get install leafpad') won't work. You'll need

Popen(['sudo', 'apt-get', 'install', 'leafpad'])

Solution 2:

Thank you. Please make it a bit clear for me. I'm just a beginner.Do you mean that, I have to set a timer with an action and then update the progress bar in accordance with the time?This is how I managed the codes according to your suggestion. Give me just a sample script or correct my mistakes please:

classUi_MainWindow(object):
        _timer = NonedefsetupUi(self, MainWindow):
            MainWindow.setObjectName(_fromUtf8("MainWindow"))
            MainWindow.resize(426, 296)
            self.centralwidget = QtGui.QWidget(MainWindow)
            self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
            self.btn = QtGui.QPushButton(self.centralwidget)
            self.btn.setGeometry(QtCore.QRect(170, 190, 81, 27))
            self.btn.setObjectName(_fromUtf8("btn"))
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtGui.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 426, 25))
            self.menubar.setObjectName(_fromUtf8("menubar"))
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtGui.QStatusBar(MainWindow)
            self.statusbar.setObjectName(_fromUtf8("statusbar"))
            MainWindow.setStatusBar(self.statusbar)

            self.retranslateUi(MainWindow)
            QtCore.QObject.connect(self.btn, QtCore.SIGNAL(_fromUtf8("clicked()")), self.runcmnd)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
        defruncmnd(self):
            self.p = Popen(['sudo', 'apt-get', 'install', 'leafpad']) 
            self._timer= QtCore.QTimer(self)
            self._timer.setSingleShot(False)
            self._timer.timeout.connect(self.pollProgress)
            self._timer.start(1000)    # Poll every second; adjust as neededdefpollProgress(self):
            output = self.p.stdout.read()
            progress = # ...Parse the output and update the progress barif progress == 100:  # Finished
                self._timer.stop()
                        self._timer = None

Post a Comment for "Display Installation Progress In Pyqt"