如何在 Qt 5.0 中集成 Python 2.7.4

如何在 Qt 5.0 中集成 Python 2.7.4

我安装了 Python 2.7.4、Qt 5.0、PyQt 4.10.4.tar.gz 和 sip 4.15.5.tar.gz。如何将 Python 与 Qt 集成?

答案1

sudo apt-get install pyqt5-dev-tools

编写示例程序

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class Form(QWidget):
    def __init__(self, parent=None):
        super(Form, self).__init__(parent)

        nameLabel = QLabel("Name:")
        self.nameLine = QLineEdit()
        self.submitButton = QPushButton("&Submit")

        buttonLayout1 = QVBoxLayout()
        buttonLayout1.addWidget(nameLabel)
        buttonLayout1.addWidget(self.nameLine)
        buttonLayout1.addWidget(self.submitButton)

        self.submitButton.clicked.connect(self.submitContact)

        mainLayout = QGridLayout()
        # mainLayout.addWidget(nameLabel, 0, 0)
        mainLayout.addLayout(buttonLayout1, 0, 1)

        self.setLayout(mainLayout)
        self.setWindowTitle("Hello Qt")

    def submitContact(self):
        name = self.nameLine.text()

        if name == "":
            QMessageBox.information(self, "Empty Field","Please enter a name and address.")
            return
        else:
            QMessageBox.information(self, "Success!","Hello \"%s\"!" % name)

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)

    screen = Form()
    screen.show()

    sys.exit(app.exec_())

将其保存为sample.py

执行此操作

$python3 sample.py

来源 :关联

答案2

sudo apt-get 安装 python-qt4

清楚,apt-get命令不需要编译,并且你pyqt不依赖于Qt

但是如果你的python是自己构建的,那么你必须pyqt自己编译和安装。

这个问题可能会有帮助!

相关内容