Search code examples
pythonpyqtqwidgetqlineedit

PyQT LineEdit Border Color


To bring the user's attention to the text field with an error (or if it is not filled) I colorize this QLineEdit) using:

red = "QWidget { background-color:#8B0000;}"
my_cool_QLineEdit.setStyleSheet(red)

That makes an entire background of this QLineEdit red. While the message is definitely sent that doesn't look too tasteful. Instead of QLineEdit's background I wonder if it is possible to turn only its borders red. What method to use? Many thanks in advance!


Solution

  • Try this:

    from PyQt4 import QtGui
    
    app = QtGui.QApplication([])
    
    w = QtGui.QWidget()
    le = QtGui.QLineEdit(w)
    lay = QtGui.QHBoxLayout()
    w.setLayout(lay)
    lay.addWidget(le)
    le.setStyleSheet("border: 1px solid red;")
    w.show()
    app.exec_()
    

    If you want to save the previous color do this:

    ss = le.styleSheet() #original saved
    le.setStyleSheet("border: 1px solid red;") #changed
    le.setStyleSheet(ss) #back to original