Find all files of a type and open them with tkinter

(New) Find all files and open them with tkinter GUI

It’s true, Python is a neat language and it allows you to create useful stuffs in few lines of code. Here is an example. In 26 lines of code you will have a GUI to look into all the file of a type in the whole drive. I think it is useful and a good way to learn tkinter by examples.

I will show you this little app to search for all files in a dir and be able to open them by doubleclicking on the name of the file in the list.

enter image description here

The code for the GUI to search and open files with tkinter and Python

The code is pretty simple and it could easily be adapted to change dir and type of file from the app. I will come back to this in another post an make it more nice and useful.

import tkinter as tk
import os

def searchfiles(extension='.txt', folder='H:\\'):
    "Create a txt file with all the file of a type"
    filelist = []
    for r, d, f in os.walk(folder):
        for file in f:
            if file.endswith(extension):
            	filelist.append(r + "\\" + file)
    
    for f in filelist:
	    lb.insert(0, f)

def open_file():
    os.startfile(lb.get(lb.curselection()[0]))

root = tk.Tk()
root.geometry("400x400")
bt = tk.Button(root, text="Search", command=lambda:searchfiles('.pdf', 'H:\\'))
bt.pack()
lb = tk.Listbox(root)
lb.pack(fill="both", expand=1)
lb.bind("<Double-Button>", lambda x: open_file())
root.mainloop()

Video with live coding to make this app to search files with tkinter

Now… a little video about this code.

P.S.: in the video there is a typo, in the last line of code the right code is root.mainloop() and not root.manloop().

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.