「已注销」 2021-05-20 20:29 采纳率: 90.9%
浏览 623
已采纳

pyqt5 如何实时格式化显示在QTextEdit

有什么办法可以让上面的标签直接格式化转换HTML 格式吗?就是不用另一个窗口展示上一个窗口的内容,

我希望一边输入一遍转换在当前的QTextEdit 标签上, 谢谢。。

源代码下方附上: 注释清晰。希望大佬帮忙解决。谢谢

 

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import markdown2
from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QApplication, QMainWindow, QTextEdit


class thread(QThread):
    def __init__(self, pa=None):
        super().__init__(pa)
        self.pa = pa
    def run(self):
        md = self.pa.te.toPlainText() # 获取标签上用户输入的内容。
        # print(md)
        newhtml = self.pa.md2html(md)   # 并传给  md2html 函数进行渲染成HTML格式
        self.pa.teeeee.setHtml(newhtml)  # 将渲染好的HTML格式富文本显示出来


class Pywindow(QMainWindow):
    def __init__(self):
        super(Pywindow, self).__init__()
        self.resize(400, 500)     # 主窗口尺寸
        self.te = QTextEdit(self)   # 标签1
        self.te.move(50, 0)
        self.te.resize(300, 200)
        self.teeeee = QTextEdit(self)# 标签2
        self.teeeee.resize(300, 200)
        self.teeeee.move(50, 220)
        self.thread1 = thread(self)  # 重写的多线程对象
        self.te.textChanged.connect(self.thread1.run)  # 当内容发生变化时触发此信号,

    def md2html(self, mdstr):  # mdstr 此函数收到的是用户输入的内容 将其转换成HTML 格式
        '''
        markdown的转换
        '''

        extras = ['code-friendly', 'fenced-code-blocks', 'footnotes', 'tables', 'code-color', 'pyshell', 'nofollow',
                  'cuddled-lists', 'header ids', 'nofollow']

        html = """
        <html lang="zh-cn">
        <head>
        <meta content="text/html; charset=utf-8" http-equiv="content-type" />
        <style>

 .hll { background-color: #ffffcc }
.c { color: #0099FF; font-style: italic } /* Comment */
.err { color: #AA0000; background-color: #FFAAAA } /* Error */
.k { color: #006699; font-weight: bold } /* Keyword */
.o { color: #555555 } /* Operator */
.ch { color: #0099FF; font-style: italic } /* Comment.Hashbang */
.cm { color: #0099FF; font-style: italic } /* Comment.Multiline */
.cp { color: #009999 } /* Comment.Preproc */
.cpf { color: #0099FF; font-style: italic } /* Comment.PreprocFile */
.c1 { color: #0099FF; font-style: italic } /* Comment.Single */
.cs { color: #0099FF; font-weight: bold; font-style: italic } /* Comment.Special */
.gd { background-color: #FFCCCC; border: 1px solid #CC0000 } /* Generic.Deleted */
.ge { font-style: italic } /* Generic.Emph */
.gr { color: #FF0000 } /* Generic.Error */
.gh { color: #003300; font-weight: bold } /* Generic.Heading */
.gi { background-color: #CCFFCC; border: 1px solid #00CC00 } /* Generic.Inserted */
.go { color: #AAAAAA } /* Generic.Output */
.gp { color: #000099; font-weight: bold } /* Generic.Prompt */
.gs { font-weight: bold } /* Generic.Strong */
.gu { color: #003300; font-weight: bold } /* Generic.Subheading */
.gt { color: #99CC66 } /* Generic.Traceback */
.kc { color: #006699; font-weight: bold } /* Keyword.Constant */
.kd { color: #006699; font-weight: bold } /* Keyword.Declaration */
.kn { color: #006699; font-weight: bold } /* Keyword.Namespace */
.kp { color: #006699 } /* Keyword.Pseudo */
.kr { color: #006699; font-weight: bold } /* Keyword.Reserved */
.kt { color: #007788; font-weight: bold } /* Keyword.Type */
.m { color: #FF6600 } /* Literal.Number */
.s { color: #CC3300 } /* Literal.String */
.na { color: #330099 } /* Name.Attribute */
.nb { color: #336666 } /* Name.Builtin */
.nc { color: #00AA88; font-weight: bold } /* Name.Class */
.no { color: #336600 } /* Name.Constant */
.nd { color: #9999FF } /* Name.Decorator */
.ni { color: #999999; font-weight: bold } /* Name.Entity */
.ne { color: #CC0000; font-weight: bold } /* Name.Exception */
.nf { color: #CC00FF } /* Name.Function */
.nl { color: #9999FF } /* Name.Label */
.nn { color: #00CCFF; font-weight: bold } /* Name.Namespace */
.nt { color: #330099; font-weight: bold } /* Name.Tag */
.nv { color: #003333 } /* Name.Variable */
.ow { color: #000000; font-weight: bold } /* Operator.Word */
.w { color: #bbbbbb } /* Text.Whitespace */
.mb { color: #FF6600 } /* Literal.Number.Bin */
.mf { color: #FF6600 } /* Literal.Number.Float */
.mh { color: #FF6600 } /* Literal.Number.Hex */
.mi { color: #FF6600 } /* Literal.Number.Integer */
.mo { color: #FF6600 } /* Literal.Number.Oct */
.sa { color: #CC3300 } /* Literal.String.Affix */
.sb { color: #CC3300 } /* Literal.String.Backtick */
.sc { color: #CC3300 } /* Literal.String.Char */
.dl { color: #CC3300 } /* Literal.String.Delimiter */
.sd { color: #CC3300; font-style: italic } /* Literal.String.Doc */
.s2 { color: #CC3300 } /* Literal.String.Double */
.se { color: #CC3300; font-weight: bold } /* Literal.String.Escape */
.sh { color: #CC3300 } /* Literal.String.Heredoc */
.si { color: #AA0000 } /* Literal.String.Interpol */
.sx { color: #CC3300 } /* Literal.String.Other */
.sr { color: #33AAAA } /* Literal.String.Regex */
.s1 { color: #CC3300 } /* Literal.String.Single */
.ss { color: #FFCC33 } /* Literal.String.Symbol */
.bp { color: #336666 } /* Name.Builtin.Pseudo */
.fm { color: #CC00FF } /* Name.Function.Magic */
.vc { color: #003333 } /* Name.Variable.Class */
.vg { color: #003333 } /* Name.Variable.Global */
.vi { color: #003333 } /* Name.Variable.Instance */
.vm { color: #003333 } /* Name.Variable.Magic */
.il { color: #FF6600 } /* Literal.Number.Integer.Long */
 table {
        font-family: verdana,arial,sans-serif;
        font-size:11px;
        color:#333333;
        border-width: 1px;
        border-color: #999999;
        border-collapse: collapse;
        }
th {
    background:#b5cfd2 url('cell-blue.jpg');
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #999999;
    }
td {
    background:#dcddc0 url('cell-grey.jpg');
    border-width: 1px;
    padding: 8px;
    border-style: solid;
    border-color: #999999;
    }
        </style>
        </head>
        <body>
            %s
        </body>
        </html>
        """

        ret = markdown2.markdown(mdstr, extras=extras)
        return html % ret
        # 转换成HTML后返回回去

if __name__ == '__main__':
    app = QApplication(sys.argv)
    pywindow = Pywindow()
    pywindow.show()
    sys.exit(app.exec_())
  • 写回答

2条回答 默认 最新

  • CSDN专家-黄老师 2021-05-20 21:44
    关注

    用正则判断你文本框的内容,如果符合<xx></xx>这里格式就触发转换过程,参考一下下面代码,判断条件可以加上正则判断

    #!/usr/bin/python3
    # -*- coding: utf-8 -*-
    import sys
    import markdown2
    from PyQt5.QtCore import QThread
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    
    
    
    class thread(QThread):
        def __init__(self, pa=None):
            super().__init__(pa)
            self.pa = pa
    
        def run(self):
            md = self.pa.te.toPlainText()  # 获取标签上用户输入的内容。
            # print(md)
            if ("#" in md and len(md)>1) or ('<' in md and '/h>' in md):
                newhtml = self.pa.md2html(md)  # 并传给  md2html 函数进行渲染成HTML格式
                print(newhtml)
                self.pa.te.setHtml(newhtml)  # 将渲染好的HTML格式富文本显示出来
                cursor = self.pa.te.textCursor()
                cursor.movePosition(QTextCursor.End)
                self.pa.te.setTextCursor(cursor)
            self.pa.status = True
    
    class Pywindow(QMainWindow):
        def __init__(self):
            super(Pywindow, self).__init__()
            self.resize(400, 500)  # 主窗口尺寸
            self.te = QTextEdit(self)  # 标签1
            self.te.move(50, 0)
            self.status = True
            self.te.resize(300, 200)
            self.teeeee = QTextEdit(self)  # 标签2
            self.teeeee.resize(300, 200)
            self.teeeee.move(50, 220)
            self.thread1 = thread(self)  # 重写的多线程对象
            self.te.textChanged.connect(self.writetext)  # 当内容发生变化时触发此信号,
    
        def writetext(self):
            if self.status:
                self.status = False
                self.thread1.run()
    
        def md2html(self, mdstr):  # mdstr 此函数收到的是用户输入的内容 将其转换成HTML 格式
            '''
            markdown的转换
            '''
            extras = ['code-friendly', 'fenced-code-blocks', 'footnotes', 'tables', 'code-color', 'pyshell', 'nofollow',
                      'cuddled-lists', 'header ids', 'nofollow']
            html = """
            <html lang="zh-cn">
            <head>
            <meta content="text/html; charset=utf-8" http-equiv="content-type" />
            <style>
    {1}
     .hll { background-color: #ffffcc }
    .c { color: #0099FF; font-style: italic } /* Comment */
    .err { color: #AA0000; background-color: #FFAAAA } /* Error */
    .k { color: #006699; font-weight: bold } /* Keyword */
    .o { color: #555555 } /* Operator */
    .ch { color: #0099FF; font-style: italic } /* Comment.Hashbang */
    .cm { color: #0099FF; font-style: italic } /* Comment.Multiline */
    .cp { color: #009999 } /* Comment.Preproc */
    .cpf { color: #0099FF; font-style: italic } /* Comment.PreprocFile */
    .c1 { color: #0099FF; font-style: italic } /* Comment.Single */
    .cs { color: #0099FF; font-weight: bold; font-style: italic } /* Comment.Special */
    .gd { background-color: #FFCCCC; border: 1px solid #CC0000 } /* Generic.Deleted */
    .ge { font-style: italic } /* Generic.Emph */
    .gr { color: #FF0000 } /* Generic.Error */
    .gh { color: #003300; font-weight: bold } /* Generic.Heading */
    .gi { background-color: #CCFFCC; border: 1px solid #00CC00 } /* Generic.Inserted */
    .go { color: #AAAAAA } /* Generic.Output */
    .gp { color: #000099; font-weight: bold } /* Generic.Prompt */
    .gs { font-weight: bold } /* Generic.Strong */
    .gu { color: #003300; font-weight: bold } /* Generic.Subheading */
    .gt { color: #99CC66 } /* Generic.Traceback */
    .kc { color: #006699; font-weight: bold } /* Keyword.Constant */
    .kd { color: #006699; font-weight: bold } /* Keyword.Declaration */
    .kn { color: #006699; font-weight: bold } /* Keyword.Namespace */
    .kp { color: #006699 } /* Keyword.Pseudo */
    .kr { color: #006699; font-weight: bold } /* Keyword.Reserved */
    .kt { color: #007788; font-weight: bold } /* Keyword.Type */
    .m { color: #FF6600 } /* Literal.Number */
    .s { color: #CC3300 } /* Literal.String */
    .na { color: #330099 } /* Name.Attribute */
    .nb { color: #336666 } /* Name.Builtin */
    .nc { color: #00AA88; font-weight: bold } /* Name.Class */
    .no { color: #336600 } /* Name.Constant */
    .nd { color: #9999FF } /* Name.Decorator */
    .ni { color: #999999; font-weight: bold } /* Name.Entity */
    .ne { color: #CC0000; font-weight: bold } /* Name.Exception */
    .nf { color: #CC00FF } /* Name.Function */
    .nl { color: #9999FF } /* Name.Label */
    .nn { color: #00CCFF; font-weight: bold } /* Name.Namespace */
    .nt { color: #330099; font-weight: bold } /* Name.Tag */
    .nv { color: #003333 } /* Name.Variable */
    .ow { color: #000000; font-weight: bold } /* Operator.Word */
    .w { color: #bbbbbb } /* Text.Whitespace */
    .mb { color: #FF6600 } /* Literal.Number.Bin */
    .mf { color: #FF6600 } /* Literal.Number.Float */
    .mh { color: #FF6600 } /* Literal.Number.Hex */
    .mi { color: #FF6600 } /* Literal.Number.Integer */
    .mo { color: #FF6600 } /* Literal.Number.Oct */
    .sa { color: #CC3300 } /* Literal.String.Affix */
    .sb { color: #CC3300 } /* Literal.String.Backtick */
    .sc { color: #CC3300 } /* Literal.String.Char */
    .dl { color: #CC3300 } /* Literal.String.Delimiter */
    .sd { color: #CC3300; font-style: italic } /* Literal.String.Doc */
    .s2 { color: #CC3300 } /* Literal.String.Double */
    .se { color: #CC3300; font-weight: bold } /* Literal.String.Escape */
    .sh { color: #CC3300 } /* Literal.String.Heredoc */
    .si { color: #AA0000 } /* Literal.String.Interpol */
    .sx { color: #CC3300 } /* Literal.String.Other */
    .sr { color: #33AAAA } /* Literal.String.Regex */
    .s1 { color: #CC3300 } /* Literal.String.Single */
    .ss { color: #FFCC33 } /* Literal.String.Symbol */
    .bp { color: #336666 } /* Name.Builtin.Pseudo */
    .fm { color: #CC00FF } /* Name.Function.Magic */
    .vc { color: #003333 } /* Name.Variable.Class */
    .vg { color: #003333 } /* Name.Variable.Global */
    .vi { color: #003333 } /* Name.Variable.Instance */
    .vm { color: #003333 } /* Name.Variable.Magic */
    .il { color: #FF6600 } /* Literal.Number.Integer.Long */
     table {
            font-family: verdana,arial,sans-serif;
            font-size:11px;
            color:#333333;
            border-width: 1px;
            border-color: #999999;
            border-collapse: collapse;
            }
    th {
        background:#b5cfd2 url('cell-blue.jpg');
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #999999;
        }
    td {
        background:#dcddc0 url('cell-grey.jpg');
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #999999;
        }
            </style>
            </head>
            <body>
                %s
            </body>
            </html>
            """
            ret = markdown2.markdown(mdstr, extras=extras)
            return html % ret
            # 转换成HTML后返回回去
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        pywindow = Pywindow()
        pywindow.show()
        sys.exit(app.exec_())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?