~juanramirez/python-snippets/pythoncore

« back to all changes in this revision

Viewing changes to wxPython/DragAndDrop.py

  • Committer: Juan
  • Date: 2010-04-24 02:46:34 UTC
  • Revision ID: jramirez@jramirez-laptop-20100424024634-muv92x40rfhzqhax
added:
  wxPython/
  wxPython/DragAndDrop.py

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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]
 
6
 
 
7
"""
 
8
Example of how to do drag and drop in python using wx
 
9
 
 
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
 
12
"""
 
13
 
 
14
 
 
15
# Import wx.Python
 
16
import wx
 
17
 
 
18
 
 
19
# Declare GUI Constants
 
20
MENU_FILE_EXIT = wx.NewId()
 
21
DRAG_SOURCE    = wx.NewId()
 
22
 
 
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
 
32
      self.obj = obj
 
33
 
 
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')
 
38
      
 
39
      
 
40
 
 
41
 
 
42
 
 
43
 
 
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)
 
49
 
 
50
       # Menu Bar
 
51
       # Create a MenuBar
 
52
       menuBar = wx.MenuBar()
 
53
       # Build a Menu Object to go into the Menu Bar
 
54
       menu1 = wx.Menu()
 
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)
 
62
       
 
63
       
 
64
 
 
65
       # Define a Text Control to recieve Dropped Text
 
66
       # Label the control
 
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)
 
75
                
 
76
 
 
77
       # Display the Window
 
78
       self.Show(True)
 
79
 
 
80
 
 
81
 
 
82
   def CloseWindow(self, event):
 
83
       """ Close the Window """
 
84
       self.Close()
 
85
 
 
86
  
 
87
 
 
88
 
 
89
 
 
90
class MyApp(wx.App):
 
91
   """ Define the Drag and Drop Example Application """
 
92
   def OnInit(self):
 
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)
 
98
      return True
 
99
 
 
100
# Declare the Application and start the Main Loop
 
101
app = MyApp(0)
 
102
app.MainLoop()
 
103
 
 
104
 
 
105
 
 
106
 
 
107
 
 
108
 
 
109