MBV
MBV

Reputation: 630

Can not return a function result if inside a python class

I expect to have a python class to handle icon string transformation to set Window icon on a GUI.

Issue: I can not return the value of a function inside a class as follows if I have some PyQt5 usage, the IDE wont let me debug and returns this error Process finished with exit code -1073740791 (0xC0000409)

Expected behavior: Call class and return PyQt5 object icon

What I have tried so far:

from PyQt5 import QtGui
import base64

class Op_Icon():
    def __init__(self):
        super(Op_Icon, self).__init__()
        "BASE64 IMAGES"
        "-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
        self.icono_nuevo = b'PHN2ZyB2aWV3Qm94PSIwIDAgOTYgOTYiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIGlkPSJJY29uc19QYXBlciIgb3ZlcmZsb3c9ImhpZGRlbiI'b'+PHBhdGggZD0iTTIzIDgyIDIzIDE0IDUxIDE0IDUxIDM1IDczIDM1IDczIDgyIDIzIDgyWk01NyAxNi41IDY5LjUgMjkgNTcgMjkgNTcgMTYuNVpNNTcgOCAxNyA4IDE3IDg4IDc5IDg4IDc5IDMwIDU3IDhaIiBzdHJva2Utd2lkdGg9IjIuNjEyMjQiLz48L3N2Zz4='

        self.dict_imagenes = {'Nuevo': self.icono_nuevo}

    def base64_icon(self, icon_string):
        """

        :param icon_string: Image as Base64 string
        :return: image objet
        """
        pm = QtGui.QPixmap()
        pm.loadFromData(base64.b64decode(icon_string))
        icon = QtGui.QIcon()
        icon.addPixmap(pm)
        return icon

    def f_icon(self, name):
        return self.base64_icon(self.dict_imagenes[name])

op = Op_Icon()
print(op.f_icon('Nuevo'))

Upvotes: 1

Views: 73

Answers (1)

eyllanesc
eyllanesc

Reputation: 244202

The numerical codes do not help to understand the cause of the error, so it is recommended that you run the script in the console to obtain a more descriptive message like the following:

QPixmap: Must construct a QGuiApplication before a QPixmap
Aborted (core dumped)

And what it points out is that you need to create a QGuiApplication since it initializes many components that are used by QPixmap (and also QIcon).

# ...

app = QtGui.QGuiApplication([])
op = Op_Icon()
print(op.f_icon('Nuevo'))

Upvotes: 3

Related Questions