Wxpython浏览或拖放文件夹 [英] Wxpython browse for or drag and drop folder

查看:40
本文介绍了Wxpython浏览或拖放文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在下面有以下代码来手动获取目录路径,我也想添加拖放操作,以便将文件夹拖放到窗口中.

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)

推荐答案

我不确定您如何将 wx.DirDialog 与拖放条目组合在一起,因为它们是两个读取程序内部文件路径的不同方法.
对于拖放条目,您可能需要定义 wx.FileDropTarget 类:

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

然后在您的框架中

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

在此示例中,您将生成一个文本控件,您可以在其中放置文件.请注意,notify方法在其 files 参数中接收的是一个列表.
如果删除文件夹,则得到的文件夹名称如下:

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

由您决定如何处理这些列表.对于该示例,我假设您正在选择文件,然后在测试控件中写入第一个文件 files [0]

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

这篇关于Wxpython浏览或拖放文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆