我想构建一个高效的文件浏览器,我的意图是只打开 4 种特定的文件类型,它正在发生,但问题是我希望在文件对话框中,只有那些文件类型应该出现,这样选择就变成了更容易,但现在发生的是所有其他文件夹也出现了,但我想要的是在文件对话框中只有那些文件应该出现我想要打开的文件类型。这个怎么做??
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()