ImportError:没有名为QtQml的模块

时间:2019-07-19 16:44:40

标签: python qml pyqt5

我正在开发一个简单的GUI应用程序。我要归档。一个是qml,另一个是python文件。 这是代码

python代码

import sys
from PyQt5.QtCore import QObject, QUrl, Qt
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine

if __name__ == "__main__":
      app = QApplication(sys.argv)
      engine = QQmlApplicationEngine()
      ctx = engine.rootContext()
      ctx.setContextProperty("main", engine)

      engine.load('test.qml')

      def myFunction():
          print 'handler called'


      button = win.findChild(QObject, "myButton")
      button.messageRequired.connect(myFunction)
      button.clicked.connect(myFunction)

      win = engine.rootObjects()[0]
      win.show()
      sys.exit(app.exec_())

qml代码

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtGraphicalEffects 1.0
import QtQuick.Dialogs 1.3
import QtQuick.Controls.Material 2.12

Window {
    id: window
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Material.theme: Material.Dark
    Material.accent: Material.Purple


    Rectangle {
        id: rectangle
        x: 385
        y: 325
        width: 315
        height: 45
        color: "#ffffff"
        radius: 18
        anchors.right: parent.right
        anchors.rightMargin: 0
        border.color: "#ffde36"
        border.width: 4

        TextInput {
            id: textInput
            x: 8
            y: 8
            width: 299
            height: 29
            text: qsTr("Text Input")
            horizontalAlignment: Text.AlignRight
            anchors.right: parent.right
            anchors.rightMargin: 8
            font.pixelSize: 20
        }
    }

    Rectangle {
        id: rectangle1
        x: -5
        width: 299
        color: "#00abdb"
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 0
        anchors.top: parent.top
        anchors.topMargin: 0
        Material.theme: Material.Dark
        Material.accent: Material.Purple


        Text {
            id: element
            x: 8
            y: 84
            width: 294
            height: 53
            color: "#ffffff"
            text: qsTr("SmartQR PRO")
            leftPadding: 1
            font.letterSpacing: -0.4
            font.bold: true
            font.pixelSize: 43
        }

        Text {
            id: element1
            x: 12
            y: 115
            width: 287
            color: "#ffffff"
            text: qsTr("SmartQR is an open source QR&Barcode Scanner. Specialy made for Linux distributions. There are many type of scanning options with that you can do your job easely and quickly. Also You can generate QR codes and Barcodes.")
            anchors.top: parent.top
            anchors.topMargin: 165
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 155
            wrapMode: Text.WrapAnywhere
            font.capitalization: Font.MixedCase
            fontSizeMode: Text.VerticalFit
            textFormat: Text.PlainText
            font.pixelSize: 16
        }

        Button {
            signal messageRequired
            id: button
            Material.accent: Material.Purple
            objectName: "myButton"
            x: 8
            y: 310
            width: 283
            height: 40
            text: qsTr("Button")
            onClicked: messageRequired()
        }
    }
}

但是当我尝试运行python文件时,它给了我这个错误。

Traceback (most recent call last):
  File "python.py", line 8, in <module>
    from PyQt5.QtQml import QQmlApplicationEngine
ImportError: No module named QtQml

我已经安装了所有带有qtqml的软件包以及所有qml模块,但没有一个解决我的问题。

  • Qt版本:5.12
  • 操作系统:Ubuntu 19.04
  • Python:3.7
  • 软件:Qt创建者

0 个答案:

没有答案