0

I am using PyCharm to work on a PyQt5 UI.

I have created a class to override a QLabel and then, when I created one object, I used it in a function.

app = QApplication([])
window = uic.loadUi("example.ui")

...
...
...

window.message = ''

def message():
    if condition == 1:
        window.message = 'You have been assigned to condition 1'
    elif condition == 2:
        window.message = 'You have been assigned to condition 2'


class ColouredLabel(QLabel):
    def setColour(self, colourString):

        self.setAutoFillBackground(True)
        myPalette = self.palette()
        myPalette.setColor(QPalette.Window, QColor(colourString))
        self.setPalette(myPalette)

lblColour = ColouredLabel(window)
lblColour.setText('RED')
lblColour.setGeometry(200, 40, 300, 80)
lblColour.setColour("yellow")


def setMessage():
    if condition == 1:
        lblColour.hide()
    else:
        lblColour.show()QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)

My question is, to use a label created through a class (like lblColour) inside a function, do I need it to convert to a window property, so that the code is like this?

...

lblColour = ColouredLabel(window)
lblColour.setText('RED')
lblColour.setGeometry(200, 40, 300, 80)
lblColour.setColour("yellow")
window.lblColour = lblColour


def setMessage():
    if window.condition == 1:
        window.lblColour.hide()
    else:
        window.lblColour.show()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
M99__08
  • 1
  • 2
  • Your question is unclear, explain yourself better – eyllanesc Jan 11 '21 at 18:22
  • If I create an object, like a label, through a class, do I need to make it a window property by adding 'window.' before its name? In my example above, I created the object "lblColour = ColouredLabel(window)". If I want to use lblColour inside a function (here, 'setMessage'), do I need to do this "window.lblColour = lblColour" and use "window.lblColour" inside the function, or I can use just "lblColour" inside the function? – M99__08 Jan 11 '21 at 19:04
  • Setting `window.label = new_label` does not necessarily replace the label since you are only signaling that" new_label "is an attribute, nothing more. From what I understand you that you want a custom QLabel to replace a QLabel placed in the .ui through Qt Designer, am I correct? If so then the solution is promotion – eyllanesc Jan 11 '21 at 19:07

0 Answers0