tkinter文本小部件带有自动标记?

发布于 2025-01-27 05:50:07 字数 341 浏览 1 评论 0原文

tkinter中是否有文本小部件自动显示文本字符串的标记?因此,例如,如果我插入:** lorem ipsum ** dolor sit _amet _amet _到此类文本窗口小部件,它将自动显示:

lorem ipsum ipsum dolor sit amet amet

,我已经尝试使用text.tag_add()方法来对文本的某些部分进行硬编码降标,但是它没有用,并且如果您可以自动这样做,则是不切实际的。因此,相反,我询问是否有一种文本小部件或文本小部件的函数/方法可以为您完成工作。

Is there a Text widget in Tkinter that automatically displays the markdown of a string of text? So for example, if I insert: **Lorem ipsum** dolor sit _amet_ to such Text widget, it automatically displays:

Lorem ipsum dolor sit amet

To achieve this, I already tried using the Text.tag_add() method to hard-code the markdown in certain parts of the text, but it didn't work somehow and it's impractical if you can do it automatically anyways. So instead I am asking if there is a type of Text widget or a function/method of the Text widget that does the work for you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北座城市 2025-02-03 05:50:07

tk.text中没有自动降低的特定方法或属性,但是可以将其作为绑定到函数的绑定。

该代码说明了如何实现自动降级。
函数Markdown每次都调用键返回被按下。

import tkinter as tk

M = tk.Tk()
T = tk.Text(M, undo = 1, block = 1, font = "{Courier New} 10")
T.grid(sticky = tk.NSEW)

T.tag_config("mark", font = "{Courier New} 10 bold")


def markdown(event):

    S, K, E = "1.0", "", "end"
    while S > "":    
        S = T.search("**", S, stopindex = "end")
        if S:
            E = T.search("**", f"{S} + 2c", stopindex = "end")
            if E:
                E = f"{E} + 2c"
                T.tag_add("mark", S, E)
                S = K = E 
            else:
                S = ""
    # remove all **
    if K:
        A = "1.0"
        while A > "":
            A = T.search("**", A, stopindex = f"{K}")
            if A:
                T.replace(A, f"{A} + 2c", "")

R = T.bind("<Return>", markdown)
T.focus()

T.insert("1.0", """**A demonstration text for automatic markdown**

A **markdown** can be achieved by **binding** a **key** to a **function**.

This has not been fully tested **but** seems to **work** ok.
**This is a


demo of multi-line markdown**.

**This is a demo of half markdown construct

""")

M.mainloop()

There are no specific methods or attributes for automatic markdown in tk.Text, however it can be implemented as a binding to a function.

This code demonstrates how automatic markdown might be achieved.
Function markdown is called every time Key-Return is pressed.

import tkinter as tk

M = tk.Tk()
T = tk.Text(M, undo = 1, block = 1, font = "{Courier New} 10")
T.grid(sticky = tk.NSEW)

T.tag_config("mark", font = "{Courier New} 10 bold")


def markdown(event):

    S, K, E = "1.0", "", "end"
    while S > "":    
        S = T.search("**", S, stopindex = "end")
        if S:
            E = T.search("**", f"{S} + 2c", stopindex = "end")
            if E:
                E = f"{E} + 2c"
                T.tag_add("mark", S, E)
                S = K = E 
            else:
                S = ""
    # remove all **
    if K:
        A = "1.0"
        while A > "":
            A = T.search("**", A, stopindex = f"{K}")
            if A:
                T.replace(A, f"{A} + 2c", "")

R = T.bind("<Return>", markdown)
T.focus()

T.insert("1.0", """**A demonstration text for automatic markdown**

A **markdown** can be achieved by **binding** a **key** to a **function**.

This has not been fully tested **but** seems to **work** ok.
**This is a


demo of multi-line markdown**.

**This is a demo of half markdown construct

""")

M.mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文