2015-01-15 2 views
2

나는 (파이썬 프로그램에서) 우분투 시스템에서 작업하고 있는데, 나는 무엇을하고 있는지 거의 모른다. 나는 폴더에서 미디어 파일 이름을 가져오고 Text 위젯을 클릭 한 다음 클릭하여 VLC Player을 엽니 다. 그냥 추가 기능을 추가하고 싶습니다. 즉, 파일 이름을 클릭하면 강조 표시되고 VLC에서 열립니다.tkinter에서 클릭했을 때 강조 표시 텍스트

어떻게 할 수 있습니까?

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

목록 상자 대신 텍스트 위젯을 사용하는 이유가 있습니까? 목록 상자에이 동작이 내장되어 있습니다. –

+0

파이썬에서 좋지 않습니다. 누군가 제가 TextBOx를 사용하려고합니다. – Fahadkalis

답변

2

줄을 강조 표시하도록 예제를 수정했습니다. 선을 강조 표시하는 방법은 here으로 설명되어 있습니다. 기본적으로 클릭 한 줄을 확인하고 강조 표시하고 vlc를 호출하는 text_click_callback을 추가했습니다. 내가 작업 할 비디오 파일이 없으므로 코드를 실행할 수 있도록 입력 폴더를 변경했습니다. 작동 원리

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

는 다음과 같습니다 :

enter image description here

하지만 브라이언 오클리 잘 생각합니다. 목록 상자가 더 좋을 것입니다. 그럼에도 불구하고 Text를 계속 사용하려면 제공된 예제와 같이 할 수 있습니다.

+0

당신의 협력에 감사드립니다 ..... 가능한 한 단일 강조 표시가되고 더블은 VLC 플레이어에서 파일을 엽니 다. – Fahadkalis

+1

@FahadUddin 네, 할 수 있습니다 그것. ' '버튼을 추가했습니다. 편집 된 코드를 살펴보십시오. – Marcin

+0

너무 많은 도움을 주셔서 감사합니다 .... 가능합니다, 강조 표시된 파일 이름은 변수에 저장할 수 있습니다 – Fahadkalis