2

I'm working on my Python program (on an Ubuntu system), and I have little idea of what I am doing: I am importing media filenames from a folder, and print it in a Text widget, and then click it to open on VLC Player. I just want to add an additional feature, that is: when I click on any filename, it should be highlight and then open on VLC.

Can you please guide me on how can I do it?

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()
nbro
  • 15,395
  • 32
  • 113
  • 196
Fahadkalis
  • 2,971
  • 8
  • 23
  • 40

1 Answers1

2

I modified your example to have the lines highlight. How to highlight a line is explained here. Basicly I added a text_click_callback that check which line is clicked and highlight it and calls vlc. I changed the input folder, to be able to execute the code, as I dont have any video files to work with.

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()

How it works is shown below:

enter image description here

But I think Bryan Oakley is right. A listbox would be better for this. Nevertheless, if you want to keep using Text, you can do as in the example provided.

Community
  • 1
  • 1
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Thanks for your coorporation.....is that possible, single will highlight and double will open file on VLC Player – Fahadkalis Jan 16 '15 at 01:13
  • 1
    @FahadUddin Yes, you can do it. Just added `` button. Please have a look at edited code. – Marcin Jan 16 '15 at 01:32
  • Thanks for your too much help n guidance....Is that possible, highlighted filename can be save in a variable – Fahadkalis Jan 16 '15 at 15:46
  • Is there a Python function that will allow me to capture the text that is currently highlighted with the cursor and store it in a variable? – Fahadkalis Jan 19 '15 at 15:16