Search code examples
pythoncolorspyqt5qlineeditqlabel

Python PyQt5: How to change the color of QLabel if QLineEdit is empty?


With

myQLineEdit.textChanged.connect(
lambda: myQLabel.setStyleSheet("QLabel { color: green}"))

I can change the text color while I'm typing. How can I change the text color if the QLineEdit is blank?


Solution

  • The textChanged signal sends the text as a parameter, so you can use that to switch between colours:

    myQLineEdit.textChanged.connect(lambda text: myQLabel.setStyleSheet(
        "QLabel { color: %s}" % ('green' if text else 'red')))