Search code examples
pythonpyqtpyinstallerfbs

Unable to package and open my PyQt GUI using Fman FBS


I am trying to package my GUI application into an application using FMan FBS. I am able to create and open the basic plain application; however, when I try to integrate my own code into the default code, once I try to run the app it closes instantly without running.

Here is the default code:

from fbs_runtime.application_context.PyQt5 import ApplicationContext
from PyQt5.QtWidgets import QMainWindow

import sys

if __name__ == '__main__':
    # 1. Instantiate ApplicationContext
    appctxt = ApplicationContext()       
    window = QMainWindow()
    window.resize(250, 150)
    window.show()
    # 2. Invoke appctxt.app.exec_()
    exit_code = appctxt.app.exec_()      
    sys.exit(exit_code)

and this works. However, my application works a lot with layouts and so I use a QWidget as my window instead of a QMainWindow. I believe this may be why the program can't be opened when packaged.

Here is a sample of my code:

class Interface:

    def __init__(self):
        self.app = QApplication([])

    def main(self):
        window = QWidget()
        window.setGeometry(550, 300, 850, 550)
        window.setWindowTitle("GUI")
        layout = QGridLayout()
        self.app.setStyle("Fusion")

        tabs = QTabWidget()
        tab1 = QWidget()
        tab2 = QWidget()
        tab3 = QWidget()
        tabs.addTab(tab1, "Tab1")
        tabs.addTab(tab2, "Tab2")
        layout1 = QGridLayout()
        layout2 = QGridLayout()

        # ...

        tab1.setLayout(layout1)
        tab2.setLayout(layout2)

        window.setLayout(layout)
        window.show()
        self.app.exec_()

I am able to run my program fine with "FBS run"; however, when actually packing the application with "FBS freeze/ FBS installer", it doesn't open properly. It does work with the default code which leads me to believe that changing it from QMainWindow to QWidget is causing it to not work


Solution

  • The logic is similar the fbs API already has a QApplication created so you must create it, in this case you just have to make the following modification to the example provided by fbs:

    from fbs_runtime.application_context.PyQt5 import ApplicationContext
    from PyQt5.QtWidgets import QWidget, QTabWidget, QGridLayout
    
    import sys
    
    class Interface:
        def main(self):
            self.window = QWidget()
            self.window.setGeometry(550, 300, 850, 550)
            self.window.setWindowTitle("GUI")
            layout = QGridLayout()
    
            tabs = QTabWidget()
            tab1 = QWidget()
            tab2 = QWidget()
            tab3 = QWidget()
            tabs.addTab(tab1, "Tab1")
            tabs.addTab(tab2, "Tab2")
            layout1 = QGridLayout()
            layout2 = QGridLayout()
    
            # ...
    
            tab1.setLayout(layout1)
            tab2.setLayout(layout2)
    
            self.window.setLayout(layout)
            self.window.show()
    
    if __name__ == '__main__':
        # 1. Instantiate ApplicationContext
        appctxt = ApplicationContext()
        interface = InterFace()
        inteface.main()
        appctxt.app.setStyle("Fusion")
        # 2. Invoke appctxt.app.exec_()
        exit_code = appctxt.app.exec_()      
        sys.exit(exit_code)