Charanya G
Charanya G

Reputation: 5

how to set the directory path in wxpython

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.

Upvotes: 1

Views: 1479

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

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()

Upvotes: 2

Related Questions