将使用Qt Designer创建的.ui文件传递给fbs进行打包

时间:2019-12-06 11:32:21

标签: python pyqt5 python-packaging fbs

我已经编写了一个Python应用程序,我想分发它。我已经在其之上构建了一个GUI,并且到目前为止运行良好。我使用以下命令来设置GUI:

qtCreatorFile = "gui.ui" 
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        # Initialize parent PyQt classes
        QtWidgets.QMainWindow.__init__(self)
        self.setupUi(self)
        (....)

应用程序以以下内容开头:

if __name__ == "__main__":
     app = QtWidgets.QApplication(sys.argv)
     window = MyApp()
     window.show()
     sys.exit(app.exec_())  

如果我正确理解,为了将'gui.ui'文件与fbs一起使用,我应该将其作为资源加载。所以我将它们用作修改:

from fbs_runtime.application_context import ApplicationContext, cached_property #added to imports)

class AppContext(ApplicationContext):           # Subclass ApplicationContext
    def run(self):
        qtCreatorFile=self.get_design()    # to get the .ui file
        Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
        window = MyApp()
        version = self.build_settings['version']
        window.setWindowTitle("EPANET parser v" + version)
        window.show()
        return self.app.exec_()

    def get_design(self):
        qtCreatorFile=self.get_resource("gui.ui") # It is in the correct src\main\resources path
        return qtCreatorFile

    @cached_property # all tutorials suggest this , but don't understand why. ???
    def main_window(self):
        return MainWindow(self)

fbs应用程序应以以下if __name__ == '__main__:'替换开头:

if __name__ == '__main__':
    appctxt = AppContext()    
    exit_code = appctxt.app.exec_()
    sys.exit(exit_code)

但是,出现以下错误: Traceback (most recent call last): File "C:\Users\....src\main\python\main.py", line 61, in <module> class MyApp(QtWidgets.QMainWindow, Ui_MainWindow): NameError: name 'Ui_MainWindow' is not defined

我了解到MyApp现在是从Ui_MainWindow类内部定义的AppContext继承的,MyApp无法引用它。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:3)

您可以使用uic.loadUi()代替uic.loadUiType(),只需通过传递.ui路径即可填充窗口

main.py

from fbs_runtime.application_context.PyQt5 import ApplicationContext, cached_property

import sys

from mainwindow import MyApp


class AppContext(ApplicationContext):
    def run(self):
        self.window.resize(640, 480)
        self.window.show()
        return appctxt.app.exec_()

    def get_design(self):
        qtCreatorFile = self.get_resource("gui.ui")
        return qtCreatorFile

    @cached_property
    def window(self):
        return MyApp(self.get_design())


if __name__ == "__main__":
    appctxt = AppContext()
    exit_code = appctxt.run()
    sys.exit(exit_code)

mainwindow.py

from PyQt5 import QtWidgets, uic


class MyApp(QtWidgets.QMainWindow):
    def __init__(self, ui, parent=None):
        super().__init__(parent)
        uic.loadUi(ui, self)
        # ...
└── src
    ├── build
    │   └── settings
    │       ├── base.json
    │       ├── linux.json
    │       └── mac.json
    └── main
        ├── icons
        │   ├── base
        │   │   ├── 16.png
        │   │   ├── 24.png
        │   │   ├── 32.png
        │   │   ├── 48.png
        │   │   └── 64.png
        │   ├── Icon.ico
        │   ├── linux
        │   │   ├── 1024.png
        │   │   ├── 128.png
        │   │   ├── 256.png
        │   │   └── 512.png
        │   ├── mac
        │   │   ├── 1024.png
        │   │   ├── 128.png
        │   │   ├── 256.png
        │   │   └── 512.png
        │   └── README.md
        ├── python
        │   ├── main.py
        │   └── mainwindow.py
        └── resources
            └── base
                └── gui.ui