pyqt5 中占位符颜色没有改变

问题描述 投票:0回答:1

我如何更改 pyqt5 中的占位符颜色我在谷歌上尝试了不同的解决方案,但它不起作用

我举了一个例子,这样你可能会更好地理解

示例

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

如何将此颜色更改为白色

python python-3.x pyqt pyqt5
1个回答
0
投票

通过 QLineEdit 的

setStyleSheet()
你就明白了。

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

希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.