qt怎么调用python程序

39次阅读
没有评论

共计 1033 个字符,预计需要花费 3 分钟才能阅读完成。

要在 Qt 中调用 Python 程序,可以使用 QProcess 类。以下是一个简单的示例代码:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
from PyQt5.QtCore import QProcess

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.button = QPushButton(" 调用 Python 程序 ", self)
        self.button.setGeometry(100, 100, 200, 50)
        self.button.clicked.connect(self.callPythonProgram)

    def callPythonProgram(self):
        pythonProcess = QProcess()
        pythonProcess.start("python", ["path_to_your_python_script.py"])
        pythonProcess.waitForFinished()
        output = pythonProcess.readAllStandardOutput()
        print(output.data().decode())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec_())

在上述代码中,我们通过创建一个 QPushButton 来触发调用 Python 程序的操作。在 callPythonProgram 函数中,我们创建一个 QProcess 对象,并使用 start 方法来启动 Python 程序,其中第一个参数是 Python 解释器的路径,第二个参数是要执行的 Python 脚本的路径。然后,我们使用 waitForFinished 方法等待 Python 程序的执行完成,并使用 readAllStandardOutput 方法读取标准输出。最后,我们将输出打印到控制台。

请确保已经安装了 PyQt5 库,并将 path_to_your_python_script.py 替换为您实际的 Python 脚本路径。运行这个 Qt 应用程序后,单击按钮将调用 Python 程序,并将输出打印到控制台。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2024-02-05发表,共计1033字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)