Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python QLineEdit文本颜色_Python_Pyqt_Qlineedit - Fatal编程技术网

python QLineEdit文本颜色

python QLineEdit文本颜色,python,pyqt,qlineedit,Python,Pyqt,Qlineedit,我正在尝试创建一个演示应用程序来演示如何更改字体颜色 我可以在QLabel和QTextEdit中完成 我找不到为QLineEdit更改前景文本颜色的方法 我尝试过的唯一不会抛出错误的方法是: color = QColorDialog.getColor(defaultHost.textColor(), pWidget, 'Get Text Color') myPalette.setColor(myPalette.WindowText, QColor(color)) 但是,文本颜色仍为黑色 是否可

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

我可以在QLabel和QTextEdit中完成

我找不到为QLineEdit更改前景文本颜色的方法

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

color = QColorDialog.getColor(defaultHost.textColor(), pWidget, 'Get Text Color')
myPalette.setColor(myPalette.WindowText, QColor(color))
但是,文本颜色仍为黑色

是否可以执行此操作?

您可以通过以下方式执行此操作:


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

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)
此代码段显示:

  • 如何找到您正在处理的小部件类型
  • 如何将
    QColor
    QColorDialog
    转换为字符串十六进制格式,以便与样式表一起使用;及
  • 当小部件不使用所需类型的调色板元素时,如何使用
    QColorDialog
在我的例子中,我使用的是
defaultHost=led\u dem.textEdit
,其中
led\u dem
是我的表单,
textEdit
是表单上的
textEdit


另外,
pWidget
是完整的小部件定义,包括
表单
实例

我为字体文本和背景解决了问题

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

这就是我不使用css的方式

Palette= QtGui.QPalette()
Palette.setColor(QtGui.QPalette.Text, QtCore.Qt.red)
self.lineEdit.setPalette(Palette)
QLineEdit有一个方法initStyleOption,initStyleOption继承QStyleOption,然后QStyleOption有一个方法qpalete。现在您可以了解使用Qpalete方法的建议


您可以访问此链接以供参考

对于我来说,这是第一次尝试:

self.LBLDefteraStatusState.setStyleSheet('color:green;')

我很想知道你为什么要花那么多时间在这个答案上,而你原来的问题已经有了一行的答案。但我怀疑,你在这个问题上的持续困难源于未能理解各种代码的作用——特别是,
背景
前景
(它们都是过时的)没有按照你认为的方式使用(你应该改为使用
基本
文本
)。“不理解”你说得对。我发现QT是一个有点令人困惑的“学科”。而且,在我所在的地方,没有人像我一样使用它。我把时间花在了答案上,因为当我在这里得到回复时,我已经(大部分)做到了很难解决。无法使用Python 2.7.x和QT 4.8.5让“Base”和“Text”在QPushbutton上工作。为什么您认为
Base
Text
可以与
QPushbutton
一起工作?您的问题是关于在
QLineEdit
中更改字体颜色。请花时间阅读文档n对于我在上一篇评论中链接到的颜色角色,随附的插图很难让您更清楚地了解按钮应该使用哪些角色。我很抱歉:我问了一个关于QLineEdit的问题,并提供了QPushbutton的代码。
Palette= QtGui.QPalette()
Palette.setColor(QtGui.QPalette.Text, QtCore.Qt.red)
self.lineEdit.setPalette(Palette)