gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 18:56:16 27 4
gpt4 key购买 nike

我正在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com