Reputation: 12687
I'd like to make a simple GUI that offers buttons which I can drag and drop into other Windows applications such that this other applications receives a certain string depending on the button chosen.
What would be the easiest GUI framework for Python that allows this drag and drop?
Upvotes: 2
Views: 2141
Reputation: 2512
Theoretically, you can use any library for which a graphical drag-and-drop designer exists. Such tools often generate markup language which the library parses, and sometimes they generate code directly. The latter is language-dependent whilst the former shouldn't be. Either way, you'll find a way of doing it with Python.
Practically, some libraries have better visual designing tools that others. WinForms designer was really elegant and seamless when I used it, so maybe IronPython? How about PyGTK, with Glade, or the aforementioned PyQt? Maybe Jython using Swing designed in NetBeans?
EDIT: Whoops, I didn't read the question properly. What you're looking for is a framework with drag-and-drop capabilities, i.e. most of them. There's lots of things to consider though, for example are the destination and source windows going to be from the same process? Will they be written with the same framework? These things could be relevant or not, depending on how it's all written.
Upvotes: 1
Reputation: 47968
Any of the UI Libraries are likely to have support for this in some fashion. In wxPython we allow List Items to be moved between various lists. Things might look like this:
class JobList(VirtualList):
def __init__(self, parent, colref = 'job_columns'):
VirtualList.__init__(self, parent, colref)
def _bind(self):
VirtualList._bind(self)
self.Bind(wx.EVT_LIST_BEGIN_DRAG, self._startDrag)
def _startDrag(self, evt):
# Create a data object to pass around.
data = wx.CustomDataObject('JobIdList')
data.SetData(str([self.data[idx]['Id'] for idx in self.selected]))
# Create the dropSource and begin the drag-and-drop.
dropSource = wx.DropSource(self)
dropSource.SetData(data)
dropSource.DoDragDrop(flags = wx.Drag_DefaultMove)
Then, there's a ListDrop class that facilitates the dropping of things onto the lists:
class ListDrop(wx.PyDropTarget):
"""
Utility class - Required to support List Drag & Drop.
Using example code from http://wiki.wxpython.org/ListControls.
"""
def __init__(self, setFn, dataType, acceptFiles = False):
wx.PyDropTarget.__init__(self)
self.setFn = setFn
# Data type to accept.
self.data = wx.CustomDataObject(dataType)
self.comp = wx.DataObjectComposite()
self.comp.Add(self.data)
if acceptFiles:
self.data2 = wx.FileDataObject()
self.comp.Add(self.data2)
self.SetDataObject(self.comp)
def OnData(self, x, y, d):
if self.GetData():
if self.comp.GetReceivedFormat().GetType() == wx.DF_FILENAME:
self.setFn(x, y, self.data2.GetFilenames())
else:
self.setFn(x, y, self.data.GetData())
return d
And finally, a List where things can be dropped:
class QueueList(VirtualList):
def __init__(self, parent, colref = 'queue_columns'):
VirtualList.__init__(self, parent, colref)
self.SetDropTarget(ListDrop(self.onJobDrop, 'JobIdList', True))
def onJobDrop(self, x, y, data):
idx, flags = self.HitTest((x, y)) #@UnusedVariable
if idx == -1: # Not dropped on a list item in the target list.
return
# Code here to handle incoming data.
Upvotes: 1