python QLineEdit 文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27432456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:47:57  来源:igfitidea点击:

python QLineEdit Text Color

pythonpyqtqlineedit

提问by user3279899

I am trying to create a demonstration app to show how to change font colors.

我正在尝试创建一个演示应用程序来展示如何更改字体颜色。

I can do it in QLabel and QTextEdit

我可以在 QLabel 和 QTextEdit 中做到

I have found no way to change the foreground text color for a QLineEdit.

我发现无法更改 QLineEdit 的前景色文本颜色。

The only thing I've tried that does not throw an error is:

我尝试过的唯一不会抛出错误的是:

color = QColorDialog.getColor(defaultHost.textColor(), pWidget, 'Get Text Color')
myPalette.setColor(myPalette.WindowText, QColor(color))

But, the text color remains black...

但是,文本颜色仍然是黑色...

Is it or is it not possible to do this?

是否可以这样做?

回答by 101

You can do it by setting the object's style sheet:

您可以通过设置对象的样式表来实现

self.my_line_edit = QtGui.QLineEdit()

self.my_line_edit.setStyleSheet("color: red;")
# or
self.my_line_edit.setStyleSheet("color: rgb(255, 0, 0);")
# or
self.my_line_edit.setStyleSheet("""
    QLabel {
        color: red;
    }
    """)

回答by user3279899

Below is a code snippet that took me two days of trial and error to figure out. I hope it helps other newbies like myself. My comments in the code should help, too.

下面是一段代码片段,我花了两天时间反复试验才弄明白。我希望它可以帮助像我这样的其他新手。我在代码中的注释也应该有所帮助。

def set_palette(pWidget, pItem):
    # Get the pallet
    myPalette = pWidget.palette()
    defaultHost = led_dem.textEdit

    if isinstance(pWidget, QPushButton):
        # NOTE: Using stylesheets will temporarily change the color dialog popups push buttons
        print "Instance Is: %s " %(pWidget.objectName())
        # Existing colors.
        bgColor = pWidget.palette().color(QPalette.Background)
        fgColor = pWidget.palette().color(QPalette.Foreground)
        # Convert the QColors to a string hex for use in the Stylesheet.
        bg = bgColor.name()
        fg = fgColor.name()

        if pItem == 'Text':
            # Use the color dialog with a dummy widget to obtain a new QColor for the parameter we are changing.
            color = QColorDialog.getColor(defaultHost.textColor(), pWidget, 'Get Text Color')
            # Convert it to a string HEX
            fg = color.name()
            # Update all parameters of interest
            pWidget.setStyleSheet('background-color: ' + bg + ';color: ' + fg)

        if pItem == 'Background':
            color = QColorDialog.getColor(defaultHost.textColor(), pWidget, 'Get Background Color')
            myPalette.setColor(myPalette.Base, QColor(color))
            bg = color.name()
            pWidget.setStyleSheet('background-color: ' + bg + ';color: ' + fg)

This snippet shows:

这个片段显示:

  • how to find what type of widget you are dealing with;
  • how to covert a QColorfrom a QColorDialoginto a string HEX format for use with a stylesheet; and
  • how to use the QColorDialogwhen the widget doesn't use a palette element of the type you need.
  • 如何找到您正在处理的小部件类型;
  • 如何将 aQColor从 aQColorDialog转换为字符串 HEX 格式以与样式表一起使用;和
  • QColorDialog当小部件不使用您需要的类型的调色板元素时如何使用。

In my case I am using defaultHost = led_dem.textEditwhere led_demis my form and textEditis a textEditon the form.

在我的情况下,我使用defaultHost = led_dem.textEditwhere led_demis my form 和textEditis a textEditon the form。

Also, pWidgetis the complete widget definition including formand instance.

此外,pWidget是完整的小部件定义,包括forminstance

回答by Hernan Ramirez

I solved for font text and background

我解决了字体文本和背景

 self.my_line_edit.setStyleSheet(
                """QLineEdit { background-color: green; color: white }""")

回答by fLY

this is how I do it not using css

这就是我不使用 css 的方式

Palette= QtGui.QPalette()
Palette.setColor(QtGui.QPalette.Text, QtCore.Qt.red)
self.lineEdit.setPalette(Palette)

QLineEdit has a method initStyleOptionand initStyleOption inherits QStyleOption, and then QStyleOption has a Method QPalette. Now you can take advatage of using QPalette methods.

QLineEdit 有一个方法initStyleOption,initStyleOption 继承了QStyleOption,然后QStyleOption 有一个Method QPalette。现在您可以利用 QPalette 方法。

you can visit this link for reference http://pyqt.sourceforge.net/Docs/PyQt4/qlineedit.html

您可以访问此链接以供参考http://pyqt.sourceforge.net/Docs/PyQt4/qlineedit.html