【问题标题】:how to change the color of specified word in python如何在python中更改指定单词的颜色
【发布时间】:2018-04-10 09:43:39
【问题描述】:

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

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

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

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

" 对于这个问题,我将不胜感激

代码:

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

功能代码:

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

【问题讨论】:

  • 请以大写字母开头并使用正确的标点符号。这对可能会回答您问题的读者更有礼貌。
  • 还提供了一个最小的工作示例。目前尚不清楚您要在这里做什么。这个问题看起来与字符串操作更相关,而不是 html 或颜色。
  • @buhtz 没有关于如何更改匹配表达式颜色的问题,因为我尝试的不是返回正确的结果
  • 不要浪费我们的时间。阅读我的第一条评论,然后再试一次!正确的标点符号!
  • @buhtz 我改进了问题

标签: python html regex file


【解决方案1】:

我更改语句后的功能工作:

  self.textEdit_PDFpreview.insertHtml(thematch)

 self.textEdit_PDFpreview.setHtml(str(thematch))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多