Reputation: 3205
I currently have this code below to manualy get a directory path, I would like to add drag and drop as well, so I could drag and drop the folder into the window.
self.pathindir1 = wx.TextCtrl(self.panel1, -1, pos=(35, 120), size=(300, 25))
self.buttonout = wx.Button(self.panel1, -1, "Open", pos=(350,118))
self.buttonout.Bind(wx.EVT_BUTTON, self.openindir1)
def openindir1(self, event):
global indir1
dlg = wx.DirDialog(self, "Choose a directory:", style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
if dlg.ShowModal() == wx.ID_OK:
indir1 = dlg.GetPath()
self.SetStatusText("Your selected directory is: %s" % indir1)
self.pathindir1.Clear()
self.pathindir1.WriteText(indir1)
Upvotes: 0
Views: 1312
Reputation: 85605
I am not sure how you want to combine a wx.DirDialog
with a drag-and-drop entry, as they are two different ways of reading a file path inside your program.
For a drag-and-drop entry you may want to define a wx.FileDropTarget
class:
class MyFileDropTarget(wx.FileDropTarget):
""""""
def __init__(self, window):
wx.FileDropTarget.__init__(self)
self.window = window
def OnDropFiles(self, x, y, filenames):
self.window.notify(filenames)
#
Then in your Frame:
class MyFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, None)
...........................
dt1 = MyFileDropTarget(self)
self.tc_files = wx.TextCtrl(self, wx.ID_ANY)
self.tc_files.SetDropTarget(dt1)
...........................
def notify(self, files):
"""Update file in testcontrol after drag and drop"""
self.tc_files.SetValue(files[0])
With this example you generate a Text Control where you can drop your file.
Note that what the notify method receives in its files
parameter is a list.
If you drop a folder you get the folder name like:
[u'C:\\Documents and Settings\\Joaquin\\Desktop\\MyFolder']
or if you drop one or more files from a folder you get:
[u'C:\\Documents and Settings\\Joaquin\\Desktop\\MyFolder\\file_1.txt',
u'C:\\Documents and Settings\\Joaquin\\Desktop\\MyFolder\\file_2.txt',
...................................................................
u'C:\\Documents and Settings\\Joaquin\\Desktop\\MyFolder\\file_n.txt']
Is up to you how to handle these lists. For the example I suppose you are selecting files and I write the first one, files[0]
, in the test control
Upvotes: 1