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()