wxPython:将文件拖到窗口中以获取文件路径 [英] wxPython: Dragging a file into window to get file path
本文介绍了wxPython:将文件拖到窗口中以获取文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将文件拖到窗口中并获取文件路径。我试过这样做:
I want to drag a file into a window and get the file path. I've tried doing this:
class CSVDropper(wx.FileDropTarget):
def __init__(self, data):
wx.FileDropTarget.__init__(self)
self.data = data
def OnDropFiles(self, x, y, filenames):
self.data = filenames
print self.data
然后在主窗口中:
# Drag & Drop
self.csv_path = None
self.drop_table = CSVDropper(self.csv_path)
self.SetDropTarget(self.drop_table)
但这什么也没做。我已经尝试运行此教程代码,但是它也没有执行任何操作。我该怎么做?
But this does nothing. I've tried running this tutorial code, but it doesn't do anything either. How do I accomplish this?
推荐答案
打印 self.data
,您应该会看到打印出的路径列表。无论如何,我写了一个教程拖放一会儿,它显示了如何执行此操作。这是我的代码的稍作修改的版本,它同时也打印出了stdout和文本控件的文件路径:
When you print self.data
, you should see a list of paths printed out. Anyway, I wrote up a tutorial on drag-n-drop a while ago which shows how to do this. Here's a slightly modified version of my code that both prints out the file paths to stdout and to a text control too:
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()
这篇关于wxPython:将文件拖到窗口中以获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文