python - 如何在 Ubuntu 上的 tkinter python 中使文本可点击

标签 python ubuntu tkinter vlc clickable

我正在 Ubuntu 上用 python 编写一个程序,从文件夹中导入和打印视频文件的名称,但它打印为简单的文本,而不是可点击的形式。

我想让它们一键点击并在视频播放器“vlc”上打开。

你能指导我这样做吗?

import io,sys,os,subprocess 
from Tkinter import *

def viewFile():
    for f in os.listdir(path):
        if f.endswith(".h264"):
            tex.insert(END,f + "\n")

if __name__ == '__main__':
    root = Tk()

    mainframe= root.title("FILE MANAGER APPLICATION")                         # Program Objective
    mainframe= root.attributes('-fullscreen', True)

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

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

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

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

最佳答案

您可以将标签添加到文本小部件中的字符范围,并且可以在标签上设置绑定(bind)。因此,一个简单的解决方案是为每个文件名创建一个唯一标签,并为该标签创建一个唯一绑定(bind)。

例如:

def viewFile():
    for f in os.listdir(path):
        if f.endswith(".h264"):
            linkname="link-" + f
            tex.insert(END,f + "\n", linkname)
            tex.tag_configure(linkname, foreground="blue", underline=True)
            tex.tag_bind(linkname, "<1>", lambda event, filename=f: openFile(filename))

这将导致一个名为 openFile 的函数用一个参数调用,即文件名。然后,您可以在该功能中做任何您想做的事情。

关于python - 如何在 Ubuntu 上的 tkinter python 中使文本可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27951158/

相关文章:

svn - 初始 SVN checkout 在浏览器中失败 : "access forbidden" in Terminal, "Error 403"

python - Ubuntu : error using cv2. imshow() 与 pyplot.plot()

java - 如何将 Spring 功能添加到 Eclipse。

python - 按钮的 Tkinter 命令不起作用

python - 如果它在 python 的字典中有相似/相同的键,如何检查值?

python - 存储 scrapy 项目以在蜘蛛完成后处理

python - matplotlib funcanimation 更新函数被第一个参数调用两次

python - Tkinter - 网格元素彼此相邻

python - 将 “stdout” 重定向到多个滚动文本小部件?

python - Google App Engine python 入站邮件 LookupError : unknown encoding