Error importing my script in python

0

I have the main file and my own function that I want to add. And he sends me the following error. The function path is fine and the function in the main file does work.

self.lbl = QLabel(self)
TypeError: arguments did not match any overloaded call:
  QLabel(parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'bool'
  QLabel(str, parent: QWidget = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()): argument 1 has unexpected type 'bool'
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import cv2

sys.path.append('./funciones')
from funciones import abrirArchivo as aF

from PIL import Image


if sys.platform.startswith('darwin'):
    rsrcPath = ":/images/mac"
else:
    rsrcPath = ":/images/win"

#clase heredada de QMAIN Window (constructor de ventanas)

class Ventana(QMainWindow,QWidget):
    def __init__(self):#Metodo constructor de la clase

        #super().__init__()
        QMainWindow.__init__(self)
        QTableWidget.__init__(self)
        #QTableWidget.__init__(self)

        self.showFullScreen()  # Tamaño inicial de la ventana 800x500
        # Barra de estado
        self.statusBar().showMessage("Bienvenid@")

        menu = self.menuBar() # Objeto menuBar
        menu_archivo = menu.addMenu("&Archivo")# Menú padre
        menu_editar = menu.addMenu("&Editar")# Menú padre



        # Agregar un elemento acción al menu_archivo --- Abrir Archivo
        menuArchivo_abrir = QAction("&Abrir",self)
        menuArchivo_abrir.setStatusTip("Abrir Archivo")
        menuArchivo_abrir.setShortcut("Ctrl+A")
        menuArchivo_abrir.triggered.connect(aF._menuArchivo_abrir)
        menu_archivo.addAction(menuArchivo_abrir)

        #self.lbl.customContextMenuRequested.connect(self.menuContextual)
        menuEditar_promedio = QAction("&Promedio", self)
        menuEditar_promedio.setStatusTip("Promedio")
        menuEditar_promedio.setShortcut("Ctrl+P")
        menuEditar_promedio.triggered.connect(self._menuEditar_promedio)
        menu_editar.addAction(menuEditar_promedio)

        menuEditar_sobelX = QAction("&Sobel X", self)
        menuEditar_sobelX.setStatusTip("Sobel X")
        menuEditar_sobelX.setShortcut("Ctrl+SX")
        menuEditar_sobelX.triggered.connect(self._menuEditar_SobelX)
        menu_editar.addAction(menuEditar_sobelX)

        # label
        #self.lbl = QLabel(self)

        #self.setContextMenuPolicy(Qt.ActionsContextMenu)

        promedioAction = QAction("Promedio", self)
        promedioAction.triggered.connect(self._menuEditar_promedio)
        self.addAction(promedioAction)

        #imagen = self._menuArchivo_abrir()
        #sobelAction = QAction("Sobel X", self)
        #sobelAction.triggered.connect(self._menuEditar_SobelX)
        #self.addAction(sobelAction)

    def menuContextual(self,point):
        #mostrando el menu contextual
        self.popMenuPromedio.exec(self.lbl.mapToGlobal(point))



    def _menuEditar_promedio(self):

        self.imagenOriginal = Image.open(self.nombreImagen)
        print(self.imagenOriginal)
        self.imagenPromedio = self.imagenOriginal

        width, height = self.imagenOriginal.size
        # [width,height]=image.shape
        ipixel = self.imagenOriginal.load()
        opixel = self.imagenPromedio.load()

        for w in range(width):
            for h in range(height):
                if w > 0 and w < width - 1 and h > 0 and h < height - 1:
                    r1, g1, b1 = ipixel[w, h]
                    r2, g2, b2 = ipixel[w, h - 1]
                    r3, g3, b3 = ipixel[w, h + 1]
                    r4, g4, b4 = ipixel[w - 1, h]
                    r5, g5, b5 = ipixel[w - 1, h - 1]
                    r6, g6, b6 = ipixel[w - 1, h + 1]
                    r7, g7, b7 = ipixel[w + 1, h]
                    r8, g8, b8 = ipixel[w + 1, h - 1]
                    r9, g9, b9 = ipixel[w + 1, h + 1]
                    r, g, b = ((r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9) / 9,
                               (g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8 + g9) / 9,
                               (b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9) / 9)

                    opixel[w, h] = (round(r), round(g), round(b), 255)

        self.imagenPromedio.save('../a2.bmp')
        self.imagenPromedio.show()

    def _menuEditar_SobelX(self,imagen):

       pass


if __name__ == '__main__':

    app = QApplication(sys.argv)##instancia para iniciar la aplicación--obligatorio pasar los argumentos argv,esto es obligatorio
    _ventana = Ventana()#creamos el objeto de la clase
    _ventana.show()#mostrando la ventana
    app.exec()#ejecutando la aplicación

My function

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import cv2


def _menuArchivo_abrir(self):
    # prueba de imagen
    self.lbl = QLabel(self)
    self.nombreImagen, _ = QFileDialog.getOpenFileName(self, "Open Image", "/home/", "Image Files (*.bmp)")

    # self.cv2_imagenOriginal = cv2.imread(self.nombreImagen)

    # print("nombre", self.cv2_imagenOriginal)
    self.pixmap = QPixmap(self.nombreImagen)
    self.lbl.setPixmap(self.pixmap)
    self.lbl.resize(800, 600)
    # self.lbl.
    self.lbl.show()
    #return self.nombreImagen
    
asked by Liszt Garcia 27.02.2017 в 22:33
source

1 answer

0

To begin with, it's not a "import" problem of the script. It is difficult to understand what your code does, so it would not have been bad for an explanation about what it does.

To be specific, what you have in your script tries to be an additional method of the widget. I will not go into this topic now, but you should make sure you know what you are doing. This function is a descriptor without references to the type of instance that uses it , reason why it does not work.

I summarize where the problematic code is:

from funciones import abrirArchivo as aF

class Ventana(QMainWindow,QWidget):
    ...
    menuArchivo_abrir.triggered.connect(aF._menuArchivo_abrir)
    ...

In the script:

def _menuArchivo_abrir(self):
    ...

When you import your script you get the _menuArchivo_abrir method that you connect to a signal.

In PyQt5 the signal triggered receives a boolean as an argument:

  

void QAction :: triggered (bool checked = false)

     

If the action is   checkable, checked is true if the action is checked, or false if the   action is unchecked.

Therefore, the _menuArchivo_abrir method receives a Boolean as the first argument, and not an instance QWidget .

Actually, _menuArchivo_abrir is a descriptor without referencing . Somehow you have to link it with the instance that interests you, something like this:

class Ventana(QMainWindow,QWidget):
    ...
    menuArchivo_abrir.triggered.connect(aF._menuArchivo_abrir.__get__(self))
    ...

That would be equivalent to what you intend:

    menuArchivo_abrir.triggered.connect(self._menuArchivo_abrir)

All we have to do is add the checked argument to the _menuArchivo_abrir method so that everything works:

def _menuArchivo_abrir(self, checked=False):
    ...
    
answered by 28.02.2017 в 14:00