WxPython限制用户远离指定目录

时间:2017-08-21 19:19:46

标签: python user-interface directory wxpython

在特定路径创建wx.DirDialog后(例如“C:\ Users \ ExampleUser \ Documents”),有没有办法限制用户远离指定的文件夹?

1 个答案:

答案 0 :(得分:1)

  

用户将选择照片的文件名

所以你不希望DirDialog而是FileDialog
我不认为这是将窗口小部件限制到特定目录的方法,但您当然可以在代码中执行此操作。 e.g。

#!/usr/bin/python
import wx
import os

class choose(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        dirname = os.getcwd()
        dlg = wx.FileDialog(self, "Choose Image file", dirname, "", "Image files (jpg)|*.jpg|All files (*.*)|*.*", style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        my_file = "No file selected"
        if dlg.ShowModal() == wx.ID_CANCEL:
            pass
        else:
            sel_dir = dlg.GetDirectory()
            if sel_dir != dirname:
                wx.MessageBox('Please choose a file from the given directory', 'Error', wx.OK | wx.ICON_ERROR)
            else:
                my_file = dlg.GetFilename()
        print "Chosen file:",my_file
        self.Destroy()

    def OnClose(self, event):
        self.Destroy()

if __name__ == '__main__':
    my_app = wx.App()
    p = choose(None)
    my_app.MainLoop()