在 Ubuntu 上在 Tkinter Python 中将突出显示的文本分配/存储为变量

Assigning /Storing Highlighted text as a variable in Tkinter Python on Ubuntu

我正在 Python Ubuntu 中编写程序,以从文件夹导入文件,然后单击左键以突出显示文件名

我正在尝试将突出显示的文本存储或分配为变量吗?

是否有 python 函数可以让我通过单击左键捕获当前突出显示的文本并将其存储为变量?

你能指导我如何完成这个任务吗?

import subprocess,os
from Tkinter import *


def text_click_callback(event):
    # an event to highlight a line when single click is done
    line_no = event.widget.index("@%s,%s linestart" % (event.x, event.y))
    #print(line_no)
    line_end = event.widget.index("%s lineend" % line_no)
    event.widget.tag_remove("highlight", 1.0, "end")
    event.widget.tag_add("highlight", line_no, line_end)
    event.widget.tag_configure("highlight", background="yellow")




def viewFile():
    tex.delete('1.0', END)

    for f in os.listdir(path):
        linkname="link-" + f
        tex.insert(END,f + "\n", linkname)
        tex.tag_configure(linkname, foreground="blue", underline=True)
        tex.tag_bind(linkname, "<Button-1>", text_click_callback )    # highlight a line



if __name__ == '__main__':

    root = Tk()
    step = LabelFrame(root,text="FILE MANAGER", font = "Arial 20 bold   italic")
    step.grid(row=1, columnspan=7, sticky='W',padx=100, pady=5, ipadx=130, ipady=25)

    Button(step,    text="ViewFile",        font = "Arial 8 bold    italic",    activebackground="turquoise",   width=30, height=5, command=viewFile).grid      (row= 6, column =3)
    Button(step,    text="Exit",            font = "Arial 8 bold    italic",    activebackground="turquoise",   width=20, height=5, command=root.quit).grid     (row= 6, column =5)

    tex = Text(master=root)                                      # TextBox For Displaying File Information
    scr=Scrollbar(root,orient =VERTICAL,command=tex.yview)
    scr.grid(row=8, column=2, rowspan=15, columnspan=1, sticky=NS)
    tex.grid(row=8, column=1, sticky=E)
    tex.config(yscrollcommand=scr.set,font=('Arial', 8, 'bold', 'italic'))

    global process
    path = os.path.expanduser("/tmp")                   # Define path To play, delete, or rename video

    root.mainloop()

您可以使用与突出显示文本相同的索引获取文本:

the_text = event.widget.get(line_no, line_end)