Using PYQT5 When clicking on Label The function is not executed

0

Generate a clcik action to a Label with an associated pixmap, when clicking on the label, only visualize the QMessageBox that you put as a reference, the function that changes the color to the pixmap does nothing. I leave the code and hopefully you can help me.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel,QMessageBox
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt
from PyQt5.QtCore import (Qt, pyqtSignal)

import LabelClickeable

from LabelClickeable import QLabelClickable



class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 Cambio de Colores con Clic '
        self.left = 10
        self.top = 10
        self.width = 1280
        self.height = 720
        self.initUI()



    def CambioColor(self,MiPixmap):
        QMessageBox.information(self, "Correcto", "Cambio de Color",   QMessageBox.Ok)
        Peda1 = QLabel(self)
        p = QPainter(MiPixmap)
        pix=QPixmap(MiPixmap)
        mask1 = pix.createMaskFromColor(QColor(221,221,221), Qt.MaskOutColor)
        p.setPen(QColor(120, 255, 255))
        p.drawPixmap(pix.rect(), mask1, mask1.rect())
        p.end()
        Peda1.setPixmap(pix)




    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        Peda1 = QLabel(self)

        pixmapPed1 = QPixmap('C:\python\proyectos\pyqt5\MainWindow\MainWindow\images\peda1n.png')


        Peda1 = QLabelClickable(self)

        Peda1.clicked.connect(lambda: self.CambioColor(pixmapPed1))

        Peda1.move(300,100)



        self.show()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
    
asked by TINCHO6666 12.09.2018 в 22:32
source

1 answer

0

It's not that the function does not do anything, it's that it assigns the Qpixmap to a tag created locally in the method itself and that has nothing to do with your original tag. The Peda1 widget that you create initUI and that you show at the start of the app is a widget different from the QLabel that you create in the CambioColor ( Peda1 = QLabel(self) ).

To this we must add that the tag created when calling CambioColor is not displayed because you do not explicitly call its show method. The first one (the one created in initUI ) is shown because Qt makes sure to show all the child widgets when calling the show method of the parent, but the second label is created after the initialization of the parent by or that this does not It happens.

If you want to modify that same tag, assign its reference to an instance attribute by which you can access the widget from any instance method such as CambioColor .

An example based on your code:

import sys

from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QMessageBox
from PyQt5.QtGui import QPainter, QColor, QPen, QIcon, QPixmap
from PyQt5.QtCore import Qt, pyqtSignal

#import LabelClickeable
#from LabelClickeable import QLabelClickable

class QLabelClickable(QLabel):
    clicked = pyqtSignal()

    def mouseReleaseEvent(self, event):
        self.clicked.emit()


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 Cambio de Colores con Clic '
        self.left = 10
        self.top = 10
        self.width = 1280
        self.height = 720
        self.pixmap_ped1 = None
        self.peda1_lbl = None
        self.initUI()


    def cambio_color(self):
        QMessageBox.information(self, "Correcto", "Cambio de Color", QMessageBox.Ok)

        pix = self.pixmap_ped1.copy()
        mask = pix.createMaskFromColor(QColor(0, 0, 0), Qt.MaskOutColor)

        p = QPainter(pix)
        p.setPen(QColor(115, 0, 153))
        p.drawPixmap(pix.rect(), mask, mask.rect())
        p.end()
        self.peda1_lbl.setPixmap(pix)


    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.pixmap_ped1 = QPixmap('image.png')
        self.peda1_lbl = QLabelClickable(self)
        self.peda1_lbl.clicked.connect(self.cambio_color)
        self.peda1_lbl.move(140, 170)
        self.peda1_lbl.setPixmap(self.pixmap_ped1)
        self.show()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

I do not know if you are going to reuse the original pixmap or not in the app, I have chosen to assign it to an instance attribute as well and use a copy of it when changing when applying the mask, but you can load it in the method from disk. new, modify it instead of creating a copy, etc. This is the image if you want to reproduce:

    
answered by 13.09.2018 в 00:23