1
# [SNIPPET_NAME: Drag and Drop with wxPython]
2
# [SNIPPET_CATEGORIES: wxPy]
3
# [SNIPPET_DESCRIPTION: Demostrates a simple drag and drop function with wxPy]
4
# [SNIPPET_AUTHOR: Juan Ramirez <jramirez@linux.com>]
5
# [SNIPPET_LICENSE: GPL]
8
Example of how to do drag and drop in python using wx
10
This is a modified example from David Woods, Wisconsin Center for Education Research <dwoods@wcer.wisc.edu>
11
here is the original http://wiki.wxpython.org/DragAndDrop
19
# Declare GUI Constants
20
MENU_FILE_EXIT = wx.NewId()
21
DRAG_SOURCE = wx.NewId()
23
# Define Drop Target class
24
class DropTarget(wx.TextDropTarget):
25
""" This object implements Drop Target functionality for any type of data """
26
def __init__(self, obj):
27
""" Initialize the Drop Target, passing in the Object Reference to
28
indicate what should receive the dropped text """
29
# Initialize the wx.TextDropTarget Object
30
wx.TextDropTarget.__init__(self)
31
# Store the Object Reference for dropped text
34
def OnDropText(self, x, y, data):
35
""" Implement Text Drop """
36
# When text is dropped, write it into the object specified
37
self.obj.WriteText(data + '\n\n')
44
class MainWindow(wx.Frame):
45
""" This window displays the GUI Widgets. """
46
def __init__(self,parent,id,title):
47
wx.Frame.__init__(self,parent,-4, title, size = (430,350), style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
48
self.SetBackgroundColour(wx.WHITE)
52
menuBar = wx.MenuBar()
53
# Build a Menu Object to go into the Menu Bar
55
menu1.Append(MENU_FILE_EXIT, "E&xit", "Quit Application")
56
# Place the Menu Item in the Menu Bar
57
menuBar.Append(menu1, "&File")
58
# Place the Menu Bar on the ap
59
self.SetMenuBar(menuBar)
60
#Define Events for the Menu Items
61
wx.EVT_MENU(self, MENU_FILE_EXIT, self.CloseWindow)
65
# Define a Text Control to recieve Dropped Text
67
wx.StaticText(self, -1, "Drag and Drop anything in here", (20, 1))
68
# Create a read-only Text Control
69
self.text2 = wx.TextCtrl(self, -1, "", pos=(10,50), size=(410,235), style = wx.TE_MULTILINE|wx.HSCROLL|wx.TE_READONLY)
70
# Make this control a Text Drop Target
71
# Create a Text Drop Target object
72
dt2 = DropTarget(self.text2)
73
# Link the Drop Target Object to the Text Control
74
self.text2.SetDropTarget(dt2)
82
def CloseWindow(self, event):
83
""" Close the Window """
91
""" Define the Drag and Drop Example Application """
93
""" Initialize the Application """
94
# Declare the Main Application Window
95
frame = MainWindow(None, -1, "Drag and Drop Example")
96
# Show the Application as the top window
97
self.SetTopWindow(frame)
100
# Declare the Application and start the Main Loop