1

I would like to set the path of the directory and it searches for that particular path and lists all the files in it.

For Examples, I need to set path of the directory A = C:\Users\Downloads, B = C:\Users\Documents, C = C:\Users\Desktop in the code. And when A is selected it needs to load the above specified directory and list all the files in listbox.

  1. How to set the directory for each elements(A,B,C..) ----> I tried using wx.DirDialog but it always loads the last selected or default directory.

  2. When any element is selected it should load the specified directory in the code and lists the files.

1
  • wx.DirDialog will work for you - look at the defaultPath param. Commented Aug 1, 2019 at 11:56

1 Answer 1

2

Here is a simple starting point.
It uses wx.FileDialog as you wish to list files not directories (wx.DirDialog).
Select a directory from a wx.Choice which activates the dialog, to list the files within that directory.
Currently the program only prints a list of selected files, I leave the loading of a listbox to you.

import wx

class choose(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Dialog")
        mychoice = ['Select a directory','/home/rolf', '/home/public','/home/public/Documents']
        panel = wx.Panel(self,-1)
        select_dir = wx.Choice(panel,-1, choices=mychoice, pos=(20,20))
        self.Bind(wx.EVT_CHOICE, self.OnSelect)
        self.dir = mychoice[0]
        select_dir.SetSelection(0)
        self.Show()

    def OnSelect(self, event):
        if event.GetSelection() == 0:
            return
        self.dir = event.GetString()
        dlg = wx.FileDialog(None, message="Choose a file/files", defaultDir = self.dir, style=wx.FD_MULTIPLE)
        if dlg.ShowModal() == wx.ID_OK:
            print('Selected files are: ', dlg.GetPaths())
        dlg.Destroy()


if __name__ == '__main__':
    my_app = wx.App()
    choose(None)
    my_app.MainLoop()
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the suggesting wx.FileDialog and it works. Now I would like to change a question little bit, here is there is no need to choose a file or directory by the user. For example, when the user gives the input 'some name', it should know which directory to be loaded to search. So may be like a table format, if this is the input [A = C:\Users\Downloads, B = C:\Users\Documents, C = C:\Users\Desktop] should be written in the code.
I don't understand what it is that you are suggesting, that is materially different to the answer provided.
sorry if its not clear. Instead the selection done by user , i would like to have a background search and listing the files. The input by the user should only be the keyword and I would like to set the path for each keyword(A,B,C,...) and when that keyword is selected in the dropdown , it should know which directory to search and list the files present in it. For example, Keywords A should navigate some directory, B should navigate other directory. which directory should the keyword navigate can be defined inside the code. dropdown and listbox works but how to set relative path for keywords.
All I get is the vague feeling that you are trying to reinvent a wheel. Can I suggest that you edit your answer and add a worked example of what it is that you are trying to achieve, without using keyword A, directory B.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.