How to change the color of selected word in a text file using python?
The below function read from file and match the requested word if its found it change the color of the expression into red.
The problem is that when i run the program the HTML tag is displayed in the textBox as text and not as a color like this.
""+searchedSTR+"
"
I will appreciate any help with this problem
code:
def readFile(self, currentFile):
self.textEdit_PDFpreview.clear()
searchedSTR = self.lineEditSearch.text()
try:
with open(currentFile) as ctf:
ctfRead = ctf.read()
fileName = os.path.basename(currentFile)
RepX = ""+st+""
thematch=re.sub(searchedSTR,RepX,ctfRead)
print("the match \n{}".format(thematch))
matches = re.findall(searchedSTR, ctfRead, re.MULTILINE | re.IGNORECASE)
print(" matches is: \n{}".format(matches))
if matches:
self.textEdit_PDFpreview.insertHtml(thematch)
else:
print("Not Found")
self.textEdit_PDFpreview.insertHtml(str("no match found"))
functional code :
import mainwindow
from PyQt5 import QtCore, QtGui, QtWidgets
class appwindow(mainwindow.Ui_MainWindow):
def __init__(self,winObj:QtWidgets.QMainWindow):
self.winObj = winObj
self.setupUi(winObj)
self.winObj.show()
self.pushButton.clicked.connect(self.changeColor)
def changeColor(self):
st ="test"
x = ""+st+""
self.textEdit.insertHtml(x)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ImainWindow = QtWidgets.QMainWindow()
ui = appwindow(ImainWindow)
sys.exit(app.exec_())
解决方案
the function work after i changed the statement :
self.textEdit_PDFpreview.insertHtml(thematch)
to
self.textEdit_PDFpreview.setHtml(str(thematch))