Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在pyqt5中运行命令行命令_Python 3.x_Command Line_Pyqt5 - Fatal编程技术网

Python 3.x 如何在pyqt5中运行命令行命令

Python 3.x 如何在pyqt5中运行命令行命令,python-3.x,command-line,pyqt5,Python 3.x,Command Line,Pyqt5,我使用的是Windows,在我的gui中,有一个按钮可以选择计算机上的文件,单击另一个按钮可以获取文件的路径并使用它运行命令行命令,如下所示: convertstuff -i "C:\file.png" -o output.jpg 这是我从pyuic获得的生成代码: from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow):

我使用的是Windows,在我的gui中,有一个按钮可以选择计算机上的文件,单击另一个按钮可以获取文件的路径并使用它运行命令行命令,如下所示:

convertstuff -i "C:\file.png" -o output.jpg
这是我从pyuic获得的生成代码:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)        
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)        
        self.pushButton_2.setObjectName("pushButton_2")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)        
        self.lineEdit.setObjectName("lineEdit")

        # event actions
        self.pushButton_2.clicked.connect(self.fileOpen)
        self.pushButton.clicked.connect(self.convert_command)


    def fileOpen(self):
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(None, "Open File")        
        self.lineEdit.setText(fileName)

    def convert_command(self):


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

您必须使用
QProcess
,如下所示:

from PyQt5 import QtCore, QtGui, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        # event actions
        central_widget = QtWidgets.QWidget()
        self.setCentralWidget(central_widget)
        lay = QtWidgets.QVBoxLayout(central_widget)

        flay = QtWidgets.QFormLayout()

        self.input_le = QtWidgets.QLineEdit()
        input_btn = QtWidgets.QPushButton("Select Input")
        input_btn.clicked.connect(self.select_input)

        self.output_le = QtWidgets.QLineEdit()
        output_btn = QtWidgets.QPushButton("Select Output")
        output_btn.clicked.connect(self.select_output)

        process_btn = QtWidgets.QPushButton("process")
        process_btn.clicked.connect(self.process)

        flay.addRow(self.input_le, input_btn)
        flay.addRow(self.output_le, output_btn)

        lay.addLayout(flay)
        lay.addWidget(process_btn)
        lay.addStretch()

    def select_input(self):
        fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self, 
            "Open File", 
            QtCore.QDir.homePath(),
            "Images (*.png *.xpm *.jpg)") 
        self.input_le.setText(fileName)

    def select_output(self):
        fileName, _ = QtWidgets.QFileDialog.getSaveFileName(self, 
            "Open File", 
            QtCore.QDir.homePath(),
            "Images (*.png *.xpm *.jpg)") 
        self.output_le.setText(fileName)

    def process(self):
        if  self.input_le.text() and self.output_le.text():
            QtCore.QProcess.startDetached(r"C:\Users\IEUser\Desktop\convertstuff.exe", 
                ["-i", self.input_le.text(), "-o", self.output_le.text()])
        else:
            print("empty arguments")


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())