简体   繁体   English

QLineEdit 更改 PlaceholderText 颜色

[英]QLineEdit change PlaceholderText color

I have a LineEdit widget in an app and its PlaceholderText changes based on the user's input.我在应用程序中有一个 LineEdit 小部件,它的 PlaceholderText 根据用户的输入发生变化。 However, I'd like for the PlaceholderText to look like normal text, ie to be black instead of grey.但是,我希望 PlaceholderText 看起来像普通文本,即黑色而不是灰色。

I've looked online but most of the results were either not precise enough for me to understand them or using different languages than Python, which makes it hard for me to implement the solution in my script.我在网上查看过,但大多数结果要么不够精确,我无法理解它们,要么使用与 Python 不同的语言,这使我很难在脚本中实现解决方案。

To change the color of the placeholderText then you have to use the QPalette:要更改 placeholderText 的颜色,您必须使用 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM