我使用的是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_())
答案 0 :(得分:0)
您必须使用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_())