首页 > 解决方案 > 如何在 QlineEdit 中更改单个字符串的颜色

问题描述

我正在使用计算器,当我单击“+”按钮时,我希望“+”为绿色而不是白色,而不改变数字的白色

计算器的图像

这是按钮“+”的代码:

def plus(self):
    splus=self.QlineEdit.text()
    self.lineEdit.setText(splus+"+")

为了让我的问题简单明了,我希望输出是这样的:

我想要的是

标签: pythonpython-3.xcolorspyqt5qlineedit

解决方案


您可以尝试使用切换小部件颜色

    splus.setStyleSheet("QLineEdit { background: rgb(xxx,xxx,xxx); selection- 
    background-color: rgb(xxx,xxx,xxx); }")

被 QLineEdit 的 clicked 事件触发为:

    splus.clicked.connect(self.toggleColors) 

希望对你有效。


推荐阅读