Search code examples
pythonpython-3.xpyqtpyqt5

placeholder color not changeing in pyqt5


how i can change placeholder color in pyqt5 i try Different solutions on google but it didn't work

I made an example so you might understand better

example

from PyQt5 import QtCore, QtWidgets
class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        Dialog.setStyleSheet("background-color: rgb(50, 50, 50);color: rgb(255, 255, 255);")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(40, 110, 311, 41))
        self.lineEdit.setStyleSheet("QLineEdit,QTextEdit {border: 2px inset rgb(37, 39, 49);border-radius: 10px;color: rgba(255, 255, 255,1);padding-left: 5px;padding-right: 5px;padding-top: 5px;padding-left: 5px;background-color: qlineargradient(spread:pad, x1:0.0600746, y1:0.926, x2:0.96, y2:0.0340909, stop:0.233831 rgb(43, 45, 56), stop:0.865672 rgba(40,40,40,0.3));}QLineEdit:hover,QTextEdit:hover {border: 2px solid rgb(49, 50, 62);}QLineEdit:focus,QTextEdit:focus {border: 2px inset rgb(85, 170, 255);background-color: rgb(43, 45, 56)}")
        self.lineEdit.setObjectName("lineEdit")
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.lineEdit.setPlaceholderText(_translate("Dialog", "how change this color to white rgb(255,255,255)"))
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

how change this color to white


Solution

  • By QLineEdit's setStyleSheet() you get it.

    from PyQt5 import QtCore, QtWidgets, QtGui
    import sys
    
    class Ui_Dialog(object):
        def setupUi(self, Dialog):
            Dialog.setObjectName("Dialog")
            Dialog.resize(400, 300)
            Dialog.setStyleSheet("background-color: rgb(50, 50, 50);color: rgb(255, 255, 255);")
            self.lineEdit = QtWidgets.QLineEdit(Dialog)
            self.lineEdit.setGeometry(QtCore.QRect(40, 110, 311, 41))
            style = '''
                QLineEdit, QTextEdit {
                    border: 2px inset rgb(37, 39, 49);
                    border-radius: 10px;color: rgba(255, 255, 255,1);
                    padding-left: 5px; 
                    padding-right: 5px; 
                    padding-top: 5px; 
                    padding-left: 5px; 
                    background-color: qlineargradient(spread:pad, x1:0.0600746, y1:0.926, x2:0.96, y2:0.0340909, stop:0.233831 rgb(43, 45, 56), stop:0.865672 rgba(40,40,40,0.3));
                }
                QLineEdit:hover,
                QTextEdit:hover {
                    border: 2px solid rgb(49, 50, 62);
                }
                QLineEdit:focus,
                QTextEdit:focus {
                    border: 2px inset rgb(85, 170, 255);
                    background-color: rgb(43, 45, 56)
                }
    
                /*  --- HERE --- */
                QLineEdit[text=\"\"]{
                    color: green;
                }
    
            '''
            self.lineEdit.setStyleSheet(style)
            self.lineEdit.setObjectName("lineEdit")
            self.retranslateUi(Dialog)
            QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        def retranslateUi(self, Dialog): 
            _translate = QtCore.QCoreApplication.translate
            Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
            self.lineEdit.setPlaceholderText(_translate("Dialog", "how change this color to white rgb(255,255,255)"))
    
    
    if __name__ == "__main__":
        
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        ui = Ui_Dialog()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    

    Hope this helps