我想让用户从特定目录中删除文件。因此我使用:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
它打开文件浏览器,用户选择一个文件。但是用户可以在此GUI窗口中浏览到其他目录。
我想阻止用户浏览到其他目录,这样他/她就无法从其他文件夹中删除文件。只允许用户从该起始目录中选择文件。
怎么做?
答案 0 :(得分:1)
我不认为标准文件对话框可以实现这一点。但你可以写自己的。只需使用树视图小部件即可显示目录中的所有文件(和相关信息)。用户可以多选文件,您可以在用户取消对话后删除它们。
答案 1 :(得分:0)
我认为你将被困为继承标准对话框的UI方式来做到这一点。但是,对于快速和脏,应该可以在循环中使用askopenfilename()。以下内容:
while True:
filename = askopenfilename()
if not filename:
raise FileDeleteAbortError()
if os.path.dirname(filename) == expected_directory:
break
tkMessageBox.showwarning() # pick another file, this one's in the wrong directory