我写了一个小脚来运行安装命令 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进度条完成,但我不知道该怎么做。
答案 0 :(得分: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'])
答案 1 :(得分: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