Unable to setIcons in pyqt5 while using fbs

169 Views Asked by At

I am unable to set Icon to my push button in my pyqt application when using with fbs. It doesn't give error no matter what path i give to QIcon function, due to which i am unable to figure out where i am going wrong

My Code

appctxt = ApplicationContext()
version = appctxt.build_settings["version"]

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Hellow World v" + version)
        w = QWidget()
        layout = QVBoxLayout()
        btn = QPushButton("press")
        btn.setIconSize(QSize(20, 20))
        btn.setIcon(QIcon("../resources/256.png"))
        layout.addWidget(btn)
        w.setLayout(layout)

        self.setCentralWidget(w)
if __name__ == '__main__':
    window = MainWindow()
    window.show()
    exit_code = appctxt.app.exec_()
    sys.exit(exit_code)

My File Structure

file structure

1

There are 1 best solutions below

0
On

FBS already has a predefined structure that allows to obtain the resources either when the script is executed or after converting it to binary. This structure indicates that "resources" must have a "base" folder where the common files for all distributions are (similar to the "icons" folder), so in your case you must restructure your application and use ApplicationContext.get_resource():

.
.
├── python
│   └── main.py
└── resources
    └── base
        ├── 1024.png
        ├── 256.png
        └── 512.png

And then just use:

btn.setIcon(QIcon(appctxt.get_resource("256.png")))