QLineEdit更改PlaceholderText颜色

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

我在应用程序中有一个LineEdit窗口小部件,其PlaceholderText根据用户的输入进行更改。但是,我希望PlaceholderText看起来像普通文本,即是黑色而不是灰色。

我看过网上,但是大多数结果要么不够精确,以至于我无法理解它们,要么使用了不同于Python的语言,这使我难以在脚本中实现该解决方案。

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

要更改占位符文本的颜色,则必须使用QPalette:

import sys

from PyQt5 import QtGui, QtWidgets


def main():

    app = QtWidgets.QApplication(sys.argv)

    w = QtWidgets.QLineEdit(placeholderText="Stack Overflow")

    pal = w.palette()
    text_color = pal.color(QtGui.QPalette.Text)
    # or
    # text_color = QtGui.QColor("black")
    pal.setColor(QtGui.QPalette.PlaceholderText, text_color)
    w.setPalette(pal)

    w.show()

    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
© www.soinside.com 2019 - 2024. All rights reserved.