0

I want to build an efficient file browser, where my intention is to open up only 4 specific file types, it is happening, but the problem is I want that in the file dialog box, only those file types should appear, so that selection becomes easier, but now what is happening is all the other folders are also appearing, but what i want is in the file dialog box only those files should appear which file types I want to open up. How to do this??

import tkinter as tk
import os
from tkinter import filedialog
window=tk.Tk()
apps=[]

def file_browser():

    for widget in frame.winfo_children():
        widget.destroy()

    filename=filedialog.askopenfilename(initialdir="/",title="Select File",
                                        filetypes=(("presentations","*.pptx"),("Word Files","*.docx"),("All PDFs","*.pdf"),
                                                   ("All text files","*.txt")))
    apps.append(filename)
    for app in apps:
        label=tk.Label(frame,text=app,bg="yellow",fg="red")
        label.pack()

def run_apps():
    for app in apps:
        os.startfile(app)

window.title("Document Finder")
canvas=tk.Canvas(window,height=500,width=500,bg="#263D42")
canvas.pack()
frame=tk.Frame(window,bg="white")
frame.place(relwidth=0.8,relheight=0.8,relx=0.1,rely=0.1)

btn1=tk.Button(window,text="Open File", padx=10, pady=5,
               fg="white",bg="#263D42",command = file_browser)
btn1.pack()

btn2=tk.Button(window,text="Run File", padx=10, pady=5,
               fg="white",bg="#263D42",command=run_apps)
btn2.pack()

window.mainloop()

1 Answers1

0

When You open your file dialog box, only those file types specified do appear. Windows File Explorer will show you all the folders, but once you open a folder, only the specified file types appear.

You are doing this right, when you open all the folders appearing only those file types specified are appearing.

DYD
  • 382
  • 2
  • 15
  • But is there any way that I can negate or cancel the effect of windows explorer, and all the other folders that are appearing don't appear, only files of required types appear?? – Debjyoti Banerjee Mar 16 '20 at 20:51
  • But what if the file your user wants is in a folder? – DYD Mar 16 '20 at 20:52
  • True.. but here other folders are also appearing, which contains nothing, as it carries other file types, so I want to remove those folders from the file dialog box. – Debjyoti Banerjee Mar 16 '20 at 20:58
  • May be the program should even be able to look inside what is there inside the folder, and then display only those folders which carries required file types.. I don't know whether it is possible or not.. So I am asking for help. – Debjyoti Banerjee Mar 16 '20 at 21:02
  • This is not really possible in File Explorer with Tkinter. The Python IDLE (which uses Tkinter) also has this "problem". I don't think it's really necessary, I never saw an open file dialog (in any program) do that. If this is really a problem in your application, you may need to make your own open file dialog without File Explorer (which might be hard), unless there is a way to do this with File Explorer and Tkinter I don't know of. – DYD Mar 16 '20 at 21:13