python - 如何使用python更改文本框中的特定字母颜色
问题描述
我需要使用 python 突出显示文本框中的特定字母。我正在使用下面的代码,但它会更改文本框中的整个文本颜色。请提出任何其他想法。
if self.dlgAttribute.ui.txtPartDesc3.text()!="":
partDescValue = self.dlgAttribute.ui.txtPartDesc3.text()
if not partDescValue in self.englishArray:
font.setPointSize(11)
self.dlgAttribute.ui.txtPartDesc3.setFont(font)
self.dlgAttribute.ui.txtPartDesc3.setStyleSheet(_fromUtf8("color: rgb(255, 162, 0);\n"
"background-color: rgb(41, 91, 170);\n"
"font: 750 10pt \"Arial\";"))
else:
font.setPointSize(11)
self.dlgAttribute.ui.txtPartDesc3.setFont(font)
self.dlgAttribute.ui.txtPartDesc3.setStyleSheet(_fromUtf8("color: rgb(0,0,0);\n"
"background-color: rgb(255, 255, 255);\n"
"font: 750 10pt \"Arial\";"))
else:
font.setPointSize(11)
self.dlgAttribute.ui.txtPartDesc3.setFont(font)
self.dlgAttribute.ui.txtPartDesc3.setStyleSheet(_fromUtf8("color: rgb(0,0,0);\n"
"background-color: rgb(255, 255, 255);\n"
"font: 750 10pt \"Arial\";"))
解决方案
一种方法是,使用只读的富文本框,然后复制输入框的纯文本内容并将您想要突出显示的文本替换为 QTextFormat 等。
https://doc.qt.io/qt-5/richtext.html
或者,更简单的方法是将文本转换为 HTML,然后通过QWebView
.
推荐阅读
- i3 - 键绑定到关机电脑
- javascript - Javascript - 为导出的类创建事件侦听器
- cookies - 移动 Safari:iframe 缺少 cookie
- node.js - Discord JS member.ban 不删除消息
- javascript - React:将 event.preventDefault() 方法添加到功能组件中
- jquery - 使用带有自定义回调/jquery 选择器的 sweetalert2
- linux - 在图形输出到达 linux 帧缓冲区之前拦截和转移图形输出,而不是使用 X11
- c# - C# 套接字客户端服务器断开连接
- python - 将 (n_samples, n_features) ndarray 转换为 (n_samples, 1) 向量数组以用作 sklearn SVM 的训练标签
- azure - 使用 facebook 记录的 Azure B2C 用户可以更改 profie 值