-
StackOverflow 文档
-
wxpython 教程
-
拖放
-
PyDropTarget
import wx
class MyURLDropTarget(wx.PyDropTarget):
def __init__(self, window):
wx.PyDropTarget.__init__(self)
self.window = window
self.data = wx.URLDataObject();
self.SetDataObject(self.data)
def OnDragOver(self, x, y, d):
return wx.DragLink
def OnData(self, x, y, d):
if not self.GetData():
return wx.DragNone
url = self.data.GetURL()
self.window.AppendText(url + "\n")
return d
class DnDPanel(wx.Panel):
""""""
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False)
# create and setup first set of widgets
lbl = wx.StaticText(self,
label="Drag some URLS from your browser here:")
lbl.SetFont(font)
self.dropText = wx.TextCtrl(
self, size=(200,200),
style=wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
dt = MyURLDropTarget(self.dropText)
self.dropText.SetDropTarget(dt)
firstSizer = self.addWidgetsToSizer([lbl, self.dropText])
# create and setup second set of widgets
lbl = wx.StaticText(self, label="Drag this URL to your browser:")
lbl.SetFont(font)
self.draggableURLText = wx.TextCtrl(self,
value="http://www.mousevspython.com")
self.draggableURLText.Bind(wx.EVT_MOTION, self.OnStartDrag)
secondSizer = self.addWidgetsToSizer([lbl, self.draggableURLText])
# Add sizers to main sizer
mainSizer = wx.BoxSizer(wx.VERTICAL)
mainSizer.Add(firstSizer, 0, wx.EXPAND)
mainSizer.Add(secondSizer, 0, wx.EXPAND)
self.SetSizer(mainSizer)
def addWidgetsToSizer(self, widgets):
"""
Returns a sizer full of widgets
"""
sizer = wx.BoxSizer(wx.HORIZONTAL)
for widget in widgets:
if isinstance(widget, wx.TextCtrl):
sizer.Add(widget, 1, wx.EXPAND|wx.ALL, 5)
else:
sizer.Add(widget, 0, wx.ALL, 5)
return sizer
def OnStartDrag(self, evt):
""""""
if evt.Dragging():
url = self.draggableURLText.GetValue()
data = wx.URLDataObject()
data.SetURL(url)
dropSource = wx.DropSource(self.draggableURLText)
dropSource.SetData(data)
result = dropSource.DoDragDrop()
class DnDFrame(wx.Frame):
""""""
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, parent=None,
title="DnD URL Tutorial", size=(800,600))
panel = DnDPanel(self)
self.Show()
if __name__ == "__main__":
app = wx.App(False)
frame = DnDFrame()
app.MainLoop()