玩蛇网提供最新Python编程技术信息以及Python资源下载!
您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容

实现qml+python打包成exe小程序问题

我写了一个非常简单的程序,使用的qml+python,使用cx_Freeze打包,然而出现了错误。
程序如下:

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_())

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;
        var tab=tabview.getTab(last)
        tab.active = true; // Now the content will be loaded
        tab.title=tab.item.name
        console.log(tabview.getTab(last).item);
        }
    }


        Button{
            text: "change tab"
            onClicked:{
        var tab=tabview.getTab(tabview.currentIndex)
                tab.title=tab.item.name

        }
    }

  }

}

setup.py

import sys
import os
from cx_Freeze import setup, Executable



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


includefiles =['main.qml','main.py','tab.qml']
packages = ['PyQt5.QtQml','PyQt5.QtNetwork']

options = {
    'build_exe': {

        'include_files':includefiles,
        'packages':packages
    }
}



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

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

错误信息如下:

请问我该怎么做呢

报错提示给的很清楚了:
File "main.py",line 15,in
win = engine.rootObjects()[0]
IndexError:list index out of range
main.py第15行
索引超出范围,越界了。

玩蛇网文章,转载请注明出处和文章网址:http://www.iplaypy.com/wenda/wd18562.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年06月02日 - 17时43分09秒 发布自玩蛇网

我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules