简单的GUI窗口拖放



我想制作一个简单的GUI,它提供可以拖放到其他Windows应用程序中的按钮,这样其他应用程序就可以根据所选按钮接收特定的字符串。

允许这种拖放的Python最简单的GUI框架是什么?

任何UI库都可能以某种方式对此提供支持。在wxPython中,我们允许在各种列表之间移动列表项。事情可能是这样的:

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)

然后,有一个ListDrop类,它可以方便地将东西拖放到列表中:

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

最后,一个可以丢弃东西的列表:

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.

PyQt前往救援。

理论上,您可以使用任何存在图形拖放设计器的库。这样的工具通常会生成标记语言,库会对其进行解析,有时还会直接生成代码。后者依赖于语言,而前者不应该依赖于语言。不管怎样,你都会找到一种使用Python的方法。

实际上,有些库拥有比其他库更好的可视化设计工具。当我使用WinForms时,它真的很优雅,无缝衔接,所以也许是IronPython?PyGTK、Glade或前面提到的PyQt怎么样?也许Jython使用了在NetBeans中设计的Swing?

编辑:哎呀,我没有把问题读清楚。您正在寻找的是一个具有拖放功能的框架,即大多数功能。不过,有很多事情需要考虑,例如,目标窗口和源窗口是否来自同一流程?它们会用相同的框架编写吗?这些东西可能是相关的,也可能不是,这取决于它是如何写的。

最新更新