1

我写了一个小脚本来运行在 ubuntu 上安装软件的命令。这里是:

    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_())

现在我想要一个显示安装进度的栏。我知道可以使用 pyqt 进度条来完成,但我不知道该怎么做。

4

2 回答 2

0

一个简单的方法是运行一个计时器,stdout定期轮询进程,并相应地更新进度条。

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

当然,需要进行一些错误检查(当网络出现故障时,用户输入错误的密码等)。

顺便说一句,Popen('sudo apt-get install leafpad')行不通。你需要

Popen(['sudo', 'apt-get', 'install', 'leafpad'])
于 2013-10-15T09:46:07.650 回答
0

谢谢你。请对我说清楚一点。我只是一个初学者。你的意思是,我必须设置一个带有动作的计时器,然后根据时间更新进度条?这就是我根据你的建议管理代码的方式。请给我一个示例脚本或纠正我的错误:

    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
于 2013-10-16T05:16:40.007 回答