2

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:
        _fromUtf8 = QtCore.QString.fromUtf8
    except AttributeError:
        def _fromUtf8(s):
            return s

    try:
        _encoding = QtGui.QApplication.UnicodeUTF8
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig, _encoding)
    except AttributeError:
        def _translate(context, text, disambig):
            return QtGui.QApplication.translate(context, text, disambig)

    class Ui_MainWindow(object):
        def setupUi(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)
        def runcmnd(self):
            p = Popen('sudo apt-get install leafpad', stdout=PIPE,stderr=PIPE, shell=True)  
            out, err = p.communicate()
            print out


        def retranslateUi(self, MainWindow):
            MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
            self.btn.setText(_translate("MainWindow", "ok", None))


    if __name__ == "__main__":
        import sys
        app = QtGui.QApplication(sys.argv)
        MainWindow = QtGui.QMainWindow()
        ui = Ui_MainWindow()
        ui.setupUi(MainWindow)
        MainWindow.show()
        sys.exit(app.exec_())

Now I want a bar which will display the installation progress. I know that it can be done using pyqt progress bar but I've no idea how to do that.

user2420437
  • 207
  • 1
  • 2
  • 13

2 Answers2

0

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

class Ui_MainWindow(object):
    _timer = None

    # ...

    def runcmnd(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 needed

    def pollProgress(self):
        output = self.p.stdout.read()
        progress = # ...Parse the output and update the progress bar
        if 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'])
uranusjr
  • 1,380
  • 12
  • 36
  • Thank you @uranusjr. Please make it a bit clear for me. I'm just a beginner.This how I managed the codes according to your suggession. Please correct my mistakes: – user2420437 Oct 15 '13 at 11:10
  • def runcmnd(self): self.p = Popen(['sudo', 'apt-get', 'install', 'leafpad']) #...skipped. Note that p is now a member variable self._timer= QtCore.QTimer(self) self._timer.setSingleShot(False) self._timer.timeout.connect(self.pollProgress) self._timer.start(1000) # Poll every second; adjust as needed – user2420437 Oct 15 '13 at 11:18
0

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:

    class Ui_MainWindow(object):
        _timer = None
        def setupUi(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)
        def runcmnd(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 needed

        def pollProgress(self):
            output = self.p.stdout.read()
            progress = # ...Parse the output and update the progress bar
            if progress == 100:  # Finished
                self._timer.stop()
                        self._timer = None
user2420437
  • 207
  • 1
  • 2
  • 13