我正在创建一个GUI,我需要将不同的文件拖放到不同的wxFilePicker控件中。 基于此示例wxPython: Dragging a file into window to get file path我能够将文件路径从一个文件获取到一个文件选择器或所有文件选择器的相同文件路径:
def updateText(self, text):
"""
Write text to the wx control
"""
self.m_filePicker1.SetPath(text)
self.m_filePicker2.SetPath(text)
self.m_filePicker3.SetPath(text)
虽然它起作用,但预计该解决方案无用。
我是否需要为每个wxfilepicker控件创建不同的面板,还是有另一种方法可以解决此问题?
提前谢谢
伊沃
这是代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
import wx
import wx.xrc
########################################################################
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 Test_Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(500, 164),
style=wx.TAB_TRAVERSAL)
bSizer122 = wx.BoxSizer(wx.VERTICAL)
sbSizer2 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, u"Model"), wx.VERTICAL)
bSizer3 = wx.BoxSizer(wx.VERTICAL)
bSizer4 = wx.BoxSizer(wx.VERTICAL)
bSizer10 = wx.BoxSizer(wx.HORIZONTAL)
bSizer12 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText1 = wx.StaticText(sbSizer2.GetStaticBox(), wx.ID_ANY, u"Model File", wx.DefaultPosition,
wx.Size(100, -1), wx.ALIGN_CENTRE)
self.m_staticText1.Wrap(-1)
bSizer12.Add(self.m_staticText1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer12, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer13 = wx.BoxSizer(wx.VERTICAL)
self.m_filePicker1 = wx.FilePickerCtrl(sbSizer2.GetStaticBox(), wx.ID_ANY, wx.EmptyString, u"Select a file",
u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer13.Add(self.m_filePicker1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer13, 1, wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, 5)
bSizer4.Add(bSizer10, 0, wx.EXPAND, 5)
bSizer11 = wx.BoxSizer(wx.HORIZONTAL)
bSizer14 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText2 = wx.StaticText(sbSizer2.GetStaticBox(), wx.ID_ANY, u"Data File", wx.DefaultPosition,
wx.Size(100, -1), wx.ALIGN_CENTRE)
self.m_staticText2.Wrap(-1)
bSizer14.Add(self.m_staticText2, 1, wx.ALL, 5)
bSizer11.Add(bSizer14, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer15 = wx.BoxSizer(wx.VERTICAL)
self.m_filePicker2 = wx.FilePickerCtrl(sbSizer2.GetStaticBox(), wx.ID_ANY, wx.EmptyString, u"Select a file",
u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer15.Add(self.m_filePicker2, 0, wx.ALL | wx.EXPAND, 5)
bSizer11.Add(bSizer15, 1, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 5)
bSizer4.Add(bSizer11, 1, wx.EXPAND, 5)
bSizer111 = wx.BoxSizer(wx.HORIZONTAL)
bSizer141 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText21 = wx.StaticText(sbSizer2.GetStaticBox(), wx.ID_ANY, u"DoF Labels", wx.DefaultPosition,
wx.Size(100, -1), wx.ALIGN_CENTRE)
self.m_staticText21.Wrap(-1)
bSizer141.Add(self.m_staticText21, 1, wx.ALL | wx.EXPAND, 5)
bSizer111.Add(bSizer141, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer151 = wx.BoxSizer(wx.VERTICAL)
file_drop_target = MyFileDropTarget(self)
self.m_filePicker3 = wx.FilePickerCtrl(sbSizer2.GetStaticBox(), wx.ID_ANY, wx.EmptyString, u"Select a file",
u"*.*", wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer151.Add(self.m_filePicker3, 0, wx.ALL | wx.EXPAND, 5)
bSizer111.Add(bSizer151, 1, wx.EXPAND, 5)
bSizer4.Add(bSizer111, 0, wx.EXPAND, 5)
bSizer3.Add(bSizer4, 0, wx.EXPAND, 5)
sbSizer2.Add(bSizer3, 1, wx.EXPAND, 5)
bSizer122.Add(sbSizer2, 0, wx.ALL | wx.EXPAND, 5)
self.SetSizer(bSizer122)
self.Layout()
self.m_filePicker1.SetDropTarget(file_drop_target)
def updateText(self, text):
"""
Write text to the wx control
"""
self.m_filePicker1.SetPath(text)
def __del__(self):
pass
class Test_Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
size=wx.Size(500, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
bSizer239 = wx.BoxSizer(wx.VERTICAL)
self.m_panel8 = Test_Panel(self)#, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
#self.m_panel8 = wx.Panel(self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL)
bSizer239.Add(self.m_panel8, 1, wx.EXPAND
, 5)
self.SetSizer(bSizer239)
self.Layout()
self.Centre(wx.BOTH)
def __del__(self):
pass
if __name__ == "__main__":
app = wx.App(redirect=False)
frame = Test_Frame(None)
app.SetTopWindow(frame)
frame.Show(True)
app.MainLoop()
答案 0 :(得分:0)
这是我解决问题的方法:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import wx.xrc
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')
self.window.updateText(filepath)
class Model_Data(wx.Panel):
def __init__(self, parent, label):
wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(-1, -1),
style=wx.TAB_TRAVERSAL)
bSizer122 = wx.BoxSizer(wx.VERTICAL)
bSizer3 = wx.BoxSizer(wx.VERTICAL)
bSizer10 = wx.BoxSizer(wx.HORIZONTAL)
bSizer12 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, label, wx.DefaultPosition, wx.Size(100, -1),
#self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, u"Model File", wx.DefaultPosition, wx.Size(100, -1),
wx.ALIGN_CENTRE)
self.m_staticText1.Wrap(-1)
bSizer12.Add(self.m_staticText1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer12, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer13 = wx.BoxSizer(wx.VERTICAL)
self.m_filePicker1 = wx.FilePickerCtrl(self, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.*",
wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer13.Add(self.m_filePicker1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer13, 1, wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, 5)
##############################################
file_drop_target = MyFileDropTarget(self)
self.m_filePicker1.SetDropTarget(file_drop_target)
##############################################
bSizer3.Add(bSizer10, 0, wx.EXPAND, 5)
bSizer122.Add(bSizer3, 0, wx.EXPAND, 5)
self.SetSizer(bSizer122)
self.Layout()
def updateText(self, text):
"""
Write text to the wx control
"""
self.m_filePicker1.SetPath(text)
def __del__(self):
pass
class Generic_Panel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(-1, -1),
style=wx.TAB_TRAVERSAL)
bSizer122 = wx.BoxSizer(wx.VERTICAL)
bSizer3 = wx.BoxSizer(wx.VERTICAL)
bSizer10 = wx.BoxSizer(wx.HORIZONTAL)
bSizer12 = wx.BoxSizer(wx.VERTICAL)
self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, u"Model File", wx.DefaultPosition, wx.Size(100, -1),
wx.ALIGN_CENTRE)
self.m_staticText1.Wrap(-1)
bSizer12.Add(self.m_staticText1, 1, wx.ALL | wx.EXPAND, 5)
bSizer10.Add(bSizer12, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer13 = wx.BoxSizer(wx.VERTICAL)
self.m_filePicker1 = wx.FilePickerCtrl(self, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.*",
wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
bSizer13.Add(self.m_filePicker1, 1, wx.ALL, 5)
bSizer10.Add(bSizer13, 0, wx.ALIGN_CENTER_VERTICAL, 5)
bSizer3.Add(bSizer10, 1, wx.EXPAND, 5)
bSizer122.Add(bSizer3, 1, wx.EXPAND, 5)
self.SetSizer(bSizer122)
self.Layout()
bSizer122.Fit(self)
def __del__(self):
pass
class Frame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
size=wx.Size(500, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)
self.SetBackgroundColour('LightGrey')
self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
bSizer239 = wx.BoxSizer(wx.VERTICAL)
sbSizer8 = wx.StaticBoxSizer(wx.StaticBox(self, wx.ID_ANY, u"Model "), wx.VERTICAL)
self.m_panel1 = Model_Data(sbSizer8.GetStaticBox(),'Model File')
sbSizer8.Add(self.m_panel1, 0, wx.EXPAND | wx.ALL, 5)
self.m_panel2 = Model_Data(sbSizer8.GetStaticBox(),'Model Data')
sbSizer8.Add(self.m_panel2, 0, wx.ALL | wx.EXPAND, 5)
self.m_panel3 = Model_Data(sbSizer8.GetStaticBox(),'DoF Labels')
# wx.Panel(sbSizer8.GetStaticBox(), wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize,
# wx.TAB_TRAVERSAL)
sbSizer8.Add(self.m_panel3, 0, wx.EXPAND | wx.ALL, 5)
bSizer239.Add(sbSizer8, 1, wx.ALL | wx.EXPAND, 5)
self.SetSizer(bSizer239)
self.Layout()
self.Centre(wx.BOTH)
def __del__(self):
pass
if __name__ == "__main__":
app = wx.App(redirect=False)
frame = Frame(None)
app.SetTopWindow(frame)
frame.Show(True)
app.MainLoop()