How to package pyqt5 program and qml into exe using cx_Freeze

780 views Asked by At

I wrote some code with Python and QML. I want to package it into an .exe but I have some issues. Here is my code:

main.qml

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Layouts 1.1

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    signal tabAdded(variant c)
    ColumnLayout{
        anchors.fill: parent
        TabView{
            visible: true
            id:tabview
            Layout.fillHeight: true
            Layout.fillWidth: true
        }
        Button{
            text: "add tab"
            onClicked:{
                var c = Qt.createComponent("tab.qml");
        tabview.addTab("tab", c);
        var last = tabview.count-1;
        tabview.getTab(last).active = true; // Now the content will be loaded
        console.log(tabview.getTab(last).item);
        }
     }
  }

}

main.py

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

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

  engine.load('main.qml')

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

setup.py

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'

options = {
    'build_exe': {
        'includes': 'atexit'
    }
}

executables = [
    Executable('main.py', base=base)
]

setup(name='simple_PyQt5',
      version='0.1',
      description='Sample cx_Freeze PyQt5 script',
      options=options,
      executables=executables
      )

I used the cx_Freeze to package the code but I got the following errors:

Traceback (most recent call last):
"File "E"\python34\lib\sit-packages\cx_Freeze\initscripts\Console.py",
line 27,in <module>
exec(code,m.__dict__)
File"main.py",line 15,in <module>
IndexError: list index out of range

What should I do?

0

There are 0 answers