[Python] Tkinter Text widget - Use clicked text in event function

Discussão em 'Python' iniciado por Stack, Agosto 14, 2025.

  1. Stack

    Stack Membro Participativo

    I currently have a a log parser that puts user infomration into a Text widget. If a line that is inserted into the Text widget has a keyword the line will be highlighted blue. If that line is clicked i'd like to use the text from that line in the event function that carries out.

    Because I am using the config for my tag to color the line blue, am I not able to also copy the clicked text?

    Example of the code:

    from tkinter import *


    def callback(event):
    window = Toplevel()
    window.overrideredirect(1)
    window.geometry("200x100+{0}+{1}".format(event.x_root-1, event.y_root-12))
    label = Label(window, justify="left", text="Username: value \nLocation: value \nAddress: value \nSecurity Level: value")
    label.grid()
    window.bind("<Leave>", lambda e: window.destroy())

    root = Tk()
    text = Text(root)
    text.insert(END, "Click here", "tag")
    text.pack()
    text.tag_config("tag", foreground="blue")
    text.tag_bind("tag", "<Button-1>", callback)

    root.mainloop()


    How can I take the username that is clicked and use it in the function? I just want to set the username to a variable and use that, im just missing how to do it.

    Continue reading...

Compartilhe esta Página