带qml的PyQt5可执行应用程序

时间:2014-05-28 04:42:16

标签: qml exe cx-freeze pyqt5

我正在尝试通过cx_Freeze构建一个简单的可执行PyQt5应用程序,但是当我启动构建的exe文件时,它告诉我一个错误,找不到qml文件

main.py

from PyQt5.QtNetwork import *
from PyQt5.QtQml import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class MainWin(object):
    def __init__(self):
        self.eng = QQmlApplicationEngine()
        self.eng.load('main.qml')
        win = self.eng.rootObjects()[0]
        win.show()

if __name__ == '__main__':
    import sys
    App = QApplication(sys.argv)
    Win = MainWin()
    sys.exit(App.exec_())

main.qml

import QtQuick 2.2
import QtQuick.Controls 1.0
ApplicationWindow {
    id: main
    width: 400
    height: 600
    color: 'grey'
 }

1 个答案:

答案 0 :(得分:3)

我找到了答案,我应该在cx_freeze setup.py中包含qtquick,如下所示:

if sys.platform == "win32":
    base = "Win32GUI"
    PYQT5_DIR = "d:/programs/Python3/lib/site-packages/PyQt5"
    include_files = [
        "qml/",
        (os.path.join(PYQT5_DIR, "qml", "QtQuick.2"), "QtQuick.2"),
        (os.path.join(PYQT5_DIR, "qml", "QtQuick"), "QtQuick"),
        (os.path.join(PYQT5_DIR, "qml", "QtGraphicalEffects"), "QtGraphicalEffects"),
    ]

setup(
    name="exe",
    version="0.9",
    description="asd",
    author="beast",
    author_email="houshao55@gmail.com",
    options={"build_exe": {"includes": ["atexit",     "sip","PyQt5.QtCore","PyQt5.QtGui","PyQt5.QtWidgets",
                                    "PyQt5.QtNetwork","PyQt5.QtOpenGL", "PyQt5.QtQml", "PyQt5.QtQuick"],
                       "include_files": include_files,
                       "excludes" : ['Tkinter'],
                       # "optimize" : 2,
                       # "compressed" : True,
                       # "include_msvcr" : True,
                   }},
executables=[
    Executable(script="main.py",
               targetName="EVTicket.exe",
               base=base)
]
)