how to set the directory path in wxpython

心不动则不痛 提交于 2019-12-11 07:49:40

问题


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:


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


来源:https://stackoverflow.com/questions/57256881/how-to-set-the-directory-path-in-wxpython

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!