Pyqt:执行命令

时间:2013-10-05 03:10:42

标签: python user-interface cmd pyqt

我正在尝试在pyqt应用程序中执行命令行,这是我到目前为止所做的:

stdouterr = os.popen4(cmd)[1].read()

简单,并且大部分它确实有效,但是当我打开一个文本文件时,pyqt程序会停止,直到文本文件关闭。有没有办法可以让我有这样的东西,而不是停止我的申请。

编辑:

好的,我几乎想通了。我目前正在这样做:

Popen(cmd, shell=True,
         stdin=None, stdout=None, stderr=None, close_fds=True)

我想要它,但有没有办法在流程完成后读取stdout和stderr?

2 个答案:

答案 0 :(得分:0)

您可以这样阅读stdoutstderr

process = subprocess.Popen(cmd, shell=True, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)

stdout, stderr = process.communicate()
print stdout
print stderr

答案 1 :(得分:0)

#my pyqt knowledge is not the best but this works for me... didn't use your example. hope you get it still.
from PyQt5.QtCore import QProcess

process = QProcess()
process.start("yourcommand")
process.waitForStarted()
process.waitForFinished()
process.readAll()
process.close()

'''
from PyQt5.QtCore import QProcess
process = QProcess()
process.start('driverquery')
process.waitForStarted()
process.waitForFinished():
process.waitForReadyRead()
tasklist = process.readAll()
process.close()
tasklist = str(tasklist).strip().split("\\r\\n")
print(tasklist)
'''