2014-02-13 141 views
0

我目前有以下代码来手动获取目录路径,我想添加拖放以及,所以我可以将文件夹拖放到窗口中。Wxpython浏览或拖放文件夹

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) 
+0

我认为拖放已经被wx.DirDialog支持,但是这个操作意味着“复制并粘贴”。也许你需要实现你自己的DirDialog –

回答

1

我不知道你想怎么一个wx.DirDialog有拖和滴入口结合,因为他们的阅读你的程序中的文件路径两种不同的方式。
对于拖和下降的条目,你可能要定义一个wx.FileDropTarget类:

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

然后在你的框架:

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

有了这个例子中,你生成一个文本控件可以丢弃你的文件。 请注意,通知方法在其files参数中收到的是一个列表。
如果你把你的文件夹名称,如文件夹:

[u'C:\\Documents and Settings\\Joaquin\\Desktop\\MyFolder'] 

,或者如果你从一个文件夹中删除一个或多个文件,您可以:

[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'] 

是你如何处理这些名单。对于这个例子,我想你正在选择文件,我在测试控件中写入第一个文件files[0]

+0

所以我已经使用了第一块代码,并保持它作为一个类,第二块我已经插入到我的当前面板,点之间的代码,但似乎无法得到这一切工作。 – speedyrazor

+0

请参阅编辑。我忘记了主要课程中的通知方法。这是从filedroptarget类中调用,并做任何你想要做的时候放弃文件 – joaquin

+0

谢谢joaquin,现在当我拖放一个文件夹我得到TypeError:所需的字符串或Unicode类型 – speedyrazor