[Python] How do I create clickable hyperlinks from markdown link format in a Tkinter Text...

Discussão em 'Python' iniciado por Stack, Abril 3, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a Tkinter application where I display text in a Text widget. The incoming text sometimes contains markdown-style links in the following format:

    [CNN Travel](https://www.cnn.com/travel/best-destinations-to-visit-2025/index.html)

    Here, the source name ("CNN Travel") is immediately followed by a URL in brackets. I want to make the source name clickable so that clicking it opens the corresponding link in the default web browser.

    I already have code in place that processes text for bold formatting using ** markers. Below is a simplified version of my current code that processes the text stream and formats bold text:

    stream = some_function()

    full_response = ""
    buffer = ""
    bold = False
    for chunk in stream:
    if chunk != "None":
    full_response += chunk
    buffer += chunk
    if "**" in buffer:
    while "**" in buffer:
    print("exist bold")
    pre, _, post = buffer.partition("**")
    append_to_chat_log(message=pre, bold=bold)
    bold = not bold
    buffer = post
    else:
    append_to_chat_log(message=buffer, bold=bold)
    buffer = ""

    def insert_with_formatting(message):
    chat_log.tag_configure("bold", font=('Helvetica', font_size, 'bold'))
    bold_pattern = re.compile(r'\*\*(.*?)\*\*')
    cursor = 0

    for match in bold_pattern.finditer(message):
    start, end = match.span()
    # Insert text before bold part
    chat_log.insert("end", message[cursor:start])
    # Insert bold text
    chat_log.insert("end", match.group(1), "bold")
    cursor = end

    # Insert any remaining text after the last bold part
    chat_log.insert("end", message[cursor:])

    def append_to_chat_log(sender=None, message=None, bold=False):
    chat_log.config(state=tk.NORMAL)
    if sender:
    chat_log.insert("end", f"{sender}\n\n", "sender")
    if message and bold:
    print("append bold")
    chat_log.tag_configure("bold", font=('Helvetica', font_size, 'bold'))
    chat_log.insert("end", message, "bold")

    if not bold and message:
    if "**" in message:
    print("if message and not bold")
    insert_with_formatting(message)
    else:
    chat_log.insert("end", message)

    chat_log.tag_config("sender", font=('Helvetica', font_size, 'bold'), foreground=TEXT_COLOR)
    chat_log.config(state=tk.DISABLED)
    chat_log.see("end")
    chat_log.update()


    My Question: How can I modify this (or add additional code) to detect markdown-style links (e.g., [CNN Travel](https://www.cnn.com/travel/best-destinations-to-visit-2025/index.html)), and make the source name clickable? When clicked, it should open the associated URL in the browser.

    I've looked into ways to create hyperlinks in Tkinter's Text widget but haven't been able to integrate it with my current text formatting logic. Any help on how to implement clickable hyperlinks in this context would be greatly appreciated.

    Continue reading...

Compartilhe esta Página