如何改变python中指定单词的颜色 [英] how to change the color of specified word in python

查看:1031
本文介绍了如何改变python中指定单词的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python更改文本文件中所选单词的颜色?

下面的函数从文件中读取并匹配请求的单词,如果它发现它改变将表达式的颜色转换为红色。

问题是,当我运行程序时,HTML标记显示在文本框中作为文本,而不是像这样的颜色。 / p>

 < b>< span style ='color:white; mso-themecolor:background1; background:black; mso- highlight:black'>+ searchingSTR +


我会很感激任何帮助这个问题

代码:



  def readFile(self,currentFile) :

self.textEdit_PDFpreview.clear()
SearchingSTR = self.lineEditSearch.text()
尝试:
以open(currentFile)作为ctf:
ctfRead = ctf.read()
fileName = os.path.basename(currentFile)
RepX =< b>< span style ='color:white; mso-themecolor:background1; background:black; mso-highlight:black'>+ st +< / span>< / b>

thematch = re.sub(searchingSTR,RepX,ctfRead)
print(the match \\\
{}。format(thematch))
matches = re.findall (searchingSTR,ctfRead,re.MULTILINE | re.IGNORECASE)
print(matches is:\\\
{}。format(matches))

如果匹配:
self.textEdit_PDFpreview.insertHtml(thematch)
else:
print(Not Found)
self.textEdit_PDFpreview.insertHtml(str(no match found))



功能代码:



 从PyQt5导入mainwindow 

导入QtCore,QtGui,QtWidgets

$ b 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)
$ b $ def defColor(self):
st =test
x =< b>< span style = '颜色:白色; MSO的themeColor相关:background1;背景:黑色; MSO的亮点:黑色' > 中+ ST + < /跨度>< / b>中

self.textEdit.insertHtml(x)


if __name__ ==__main__:

导入sys
app = QtWidgets.QApplication(sys.argv)
ImainWindow = QtWidgets.QMainWindow()
ui = appwindow(ImainWindow)
sys.exit(app.exec_())


解决方案

  self.textEdit_PDFpreview.insertHtml(thematch)

  self.textEdit_PDFpreview.setHtml(str(thematch))


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.

 "<b><span style='color:white;mso-themecolor:background1;background:black;mso-highlight:black'>"+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 = "<b><span style='color:white;mso-themecolor:background1;background:black;mso-highlight:black'>"+st+"</span></b>"

                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 = "<b><span style='color:white;mso-themecolor:background1;background:black;mso-highlight:black'>"+st+"</span></b>"

                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))

这篇关于如何改变python中指定单词的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆