我想知道是否有一种方法可以在Tkinter中制作可点击的文本。 也许就像您在游戏的标题屏幕上看到的那样,然后将鼠标悬停在文本上,文本本身就会更改颜色/突出显示。 我所要做的就是执行另一个功能。 这些事情都有可能吗? 谢谢! ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在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()
您可以将标签添加到文本小部件中的字符范围,并且可以在标签上设置绑定。 因此,一种简单的解决方案是为每个文件名创建一个唯一的标签,并为该标签创建一个唯一的绑定。
例如:
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
的函数。 然后,您可以在该函数中执行所需的任何操作。
最后尝试此行以在VLC上打开视频
tex.tag_bind(linkname, "<1>", lambda event, filename =path+'/'+f: subprocess.call(['vlc',filename]))
一种简单的方法是遍历所有文件,并为每个文件创建一个按钮,其文件名如下所示
fram = Frame(root)
files = os.listdir(path):
for f in range(0, len(files), 1):
if files[f].endswith(".h264"):
name = files[f].split(".")
b = Button(fram, text=name[0])
b.bind('<Button>', openV)
b.grid(row=f, column=1)
现在您的openV
功能应该看起来像这样才能实际打开文件
def openV(event):
b = event.widget()
properties = b.config() #get all config for this widget, conveniently the only value right now is text
n = properties[0]
bashCommand = "vlc " + n + ".h264" # this is the command to actually be exectuted
os.system(bashCommand)
确保您的系统上安装了python-swap, os.system()
会出现错误,您不需要具有唯一的ID,因为在同一个文件夹中不存在两个具有相同名称和扩展名的文件!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.