wxpython图形界面读取文件_wxPython:将文件拖到窗口中以获取文件路径

打印self.data时,您应该看到打印出的路径列表.无论如何,我刚刚在拖拽上写了一个

tutorial,它显示了如何做到这一点.这是我的代码的略微修改版本,它既打印到stdout的文件路径也打印到文本控件:

import wx

########################################################################

class MyFileDropTarget(wx.FileDropTarget):

""""""

#----------------------------------------------------------------------

def __init__(self, window):

"""Constructor"""

wx.FileDropTarget.__init__(self)

self.window = window

#----------------------------------------------------------------------

def OnDropFiles(self, x, y, filenames):

"""

When files are dropped, write where they were dropped and then

the file paths themselves

"""

self.window.SetInsertionPointEnd()

self.window.updateText("\n%d file(s) dropped at %d,%d:\n" %

(len(filenames), x, y))

print filenames

for filepath in filenames:

self.window.updateText(filepath + '\n')

########################################################################

class DnDPanel(wx.Panel):

""""""

#----------------------------------------------------------------------

def __init__(self, parent):

"""Constructor"""

wx.Panel.__init__(self, parent=parent)

file_drop_target = MyFileDropTarget(self)

lbl = wx.StaticText(self, label="Drag some files here:")

self.fileTextCtrl = wx.TextCtrl(self,

style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)

self.fileTextCtrl.SetDropTarget(file_drop_target)

sizer = wx.BoxSizer(wx.VERTICAL)

sizer.Add(lbl, 0, wx.ALL, 5)

sizer.Add(self.fileTextCtrl, 1, wx.EXPAND|wx.ALL, 5)

self.SetSizer(sizer)

#----------------------------------------------------------------------

def SetInsertionPointEnd(self):

"""

Put insertion point at end of text control to prevent overwriting

"""

self.fileTextCtrl.SetInsertionPointEnd()

#----------------------------------------------------------------------

def updateText(self, text):

"""

Write text to the text control

"""

self.fileTextCtrl.WriteText(text)

########################################################################

class DnDFrame(wx.Frame):

""""""

#----------------------------------------------------------------------

def __init__(self):

"""Constructor"""

wx.Frame.__init__(self, parent=None, title="DnD Tutorial")

panel = DnDPanel(self)

self.Show()

#----------------------------------------------------------------------

if __name__ == "__main__":

app = wx.App(False)

frame = DnDFrame()

app.MainLoop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值