Python 在tkinter中单击时突出显示文本
我正在开发Python程序(在Ubuntu系统上),我对自己正在做的事情几乎一无所知:我正在从文件夹导入媒体文件名,并将其打印到Python 在tkinter中单击时突出显示文本,python,ubuntu,tkinter,click,highlight,Python,Ubuntu,Tkinter,Click,Highlight,我正在开发Python程序(在Ubuntu系统上),我对自己正在做的事情几乎一无所知:我正在从文件夹导入媒体文件名,并将其打印到文本小部件中,然后单击它在VLC播放器上打开。 我只想添加一个附加功能,即:当我单击任何文件名时,它应该高亮显示,然后在VLC上打开 你能告诉我怎么做吗 import subprocess,os from Tkinter import * def viewFile(): tex.delete('1.0', END) for f in os.listdi
文本
小部件中,然后单击它在VLC播放器
上打开。
我只想添加一个附加功能,即:当我单击任何文件名时,它应该高亮显示,然后在VLC上打开
你能告诉我怎么做吗
import subprocess,os
from Tkinter import *
def viewFile():
tex.delete('1.0', END)
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 =path+'/'+f: subprocess.call(['vlc',filename])) # Video play on VLC Player
if __name__ == '__main__':
root = Tk()
step= root.attributes('-fullscreen', True)
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("~/python") # Define path To play, delete, or rename video
root.mainloop()
导入子流程,操作系统
从Tkinter进口*
def viewFile():
tex.delete('1.0',结束)
对于os.listdir(路径)中的f:
如果f.endswith('.h264'):
linkname=“link-”+f
tex.insert(结束,f+“\n”,链接名)
tex.tag_configure(linkname,foreground=“blue”,underline=True)
tex.tag_bind(linkname,”,lambda事件,filename=path+'/'+f:subprocess.call(['vlc',filename]))#在vlc播放器上进行视频播放
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root=Tk()
step=root.attributes('-fullscreen',True)
步骤=LabelFrame(根,text=“文件管理器”,font=“Arial 20 bold italic”)
步骤网格(行=1,列span=7,粘滞=W',padx=100,pady=5,ipadx=130,ipady=25)
按钮(step,text=“ViewFile”,font=“Arial 8 bold italic”,activebackground=“turquoise”,宽度=30,高度=5,命令=ViewFile)。网格(行=6,列=3)
按钮(step,text=“Exit”,font=“Arial 8 bold italic”,activebackground=“turquoise”,宽度=20,高度=5,命令=root.quit)。网格(行=6,列=5)
tex=Text(master=root)#用于显示文件信息的文本框
scr=滚动条(根,方向=垂直,命令=tex.yview)
scr.grid(行=8,列=2,行span=15,列span=1,粘性=NS)
tex.grid(行=8,列=1,粘性=E)
tex.config(yscrollcommand=scr.set,font=('Arial',8',bold',italic'))
全球进程
path=os.path.expanduser(“~/python”)#定义播放、删除或重命名视频的路径
root.mainloop()
我修改了您的示例以突出显示这些行。说明了如何突出显示线条。基本上,我添加了一个
text\u click\u callback
,用于检查单击了哪一行并突出显示它并调用vlc。我更改了输入文件夹,以便能够执行代码,因为我没有任何视频文件可以处理
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):
#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, "<Button-1>", text_click_callback ) # highlight a line
tex.tag_bind(linkname, "<Double-Button-1>", lambda event, filename =path+'/'+f: subprocess.call(['vlc',filename]) ) # Video play on VLC Player
if __name__ == '__main__':
root = Tk()
#step= root.attributes('-fullscreen', True)
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()
导入子流程,操作系统
从Tkinter进口*
def text_click_回调(事件):
#单击完成时高亮显示一行的事件
行号=event.widget.index(“@%s,%s linestart”%(event.x,event.y))
#打印(行号)
line\u end=event.widget.index(“%s lineend”%line\u no)
event.widget.tag_remove(“highlight”,1.0,“end”)
event.widget.tag\u add(“突出显示”,行号,行结束)
event.widget.tag_configure(“highlight”,background=“yellow”)
def viewFile():
tex.delete('1.0',结束)
对于os.listdir(路径)中的f:
#如果f.endswith('.h264'):
linkname=“link-”+f
tex.insert(结束,f+“\n”,链接名)
tex.tag_configure(linkname,foreground=“blue”,underline=True)
tex.tag_bind(linkname,“,text_click_callback)#高亮显示一行
tex.tag_bind(linkname,”,lambda事件,filename=path+'/'+f:subprocess.call(['vlc',filename]))#在vlc播放器上进行视频播放
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root=Tk()
#step=root.attributes('-fullscreen',True)
步骤=LabelFrame(根,text=“文件管理器”,font=“Arial 20 bold italic”)
步骤网格(行=1,列span=7,粘滞=W',padx=100,pady=5,ipadx=130,ipady=25)
按钮(step,text=“ViewFile”,font=“Arial 8 bold italic”,activebackground=“turquoise”,宽度=30,高度=5,命令=ViewFile)。网格(行=6,列=3)
按钮(step,text=“Exit”,font=“Arial 8 bold italic”,activebackground=“turquoise”,宽度=20,高度=5,命令=root.quit)。网格(行=6,列=5)
tex=Text(master=root)#用于显示文件信息的文本框
scr=滚动条(根,方向=垂直,命令=tex.yview)
scr.grid(行=8,列=2,行span=15,列span=1,粘性=NS)
tex.grid(行=8,列=1,粘性=E)
tex.config(yscrollcommand=scr.set,font=('Arial',8',bold',italic'))
全球进程
path=os.path.expanduser(“/tmp”)#定义播放、删除或重命名视频的路径
root.mainloop()
其工作原理如下所示:
但我认为布莱恩·奥克利是对的。一个列表框会更好。不过,如果您想继续使用文本,可以按照提供的示例执行。使用文本小部件而不是列表框有什么原因吗?listbox内置了这种行为。我不擅长Python,有人建议我使用TextBOx,这是你们公司的yThanks…..是否可能,single将突出显示,double将在VLC上打开文件Player@FahadUddin是的,你能做到。刚刚添加了
按钮。请查看已编辑的代码。感谢您的帮助和指导…是否可能,突出显示的文件名可以保存在变量中?是否有Python函数允许我捕获当前用光标突出显示的文本并将其存储在变量中?