qml - 使用 qml 的 PyQt5 可执行应用程序

标签 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'
 }

最佳答案

我找到了答案,我应该在 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="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a72756f69727b752f2f5a7d777b737634797577" rel="noreferrer noopener nofollow">[email protected]</a>",
    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)
]
)

关于qml - 使用 qml 的 PyQt5 可执行应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23902968/

相关文章:

qt - 如何在 qml 中触发显式 UI 更新?

qt - QML 动态列宽

python - 如何使用 PyQt5 将 'Qt.WA_X11NetWmWindowTypeDesktop' 属性应用到我的 QML 窗口

qt - QML View 中的 Foreach 委托(delegate)

python - .py 仅转换为一个 .exe 文件

c++ - 如何运行Turbo C++输出的.exe文件?

python-3.x - 如何使用 cx_freeze 在 python 中创建 .EXE 文件

java - 使用允许安装目录选择的 Netbeans 创建 .exe

python - 如何向 cx_Freeze 可执行文件添加图标?

python - cx_卡住。安装后如何安装服务和执行脚本