简单的Windows拖放GUI

2 投票
3 回答
2062 浏览
提问于 2025-04-17 10:42

我想做一个简单的图形界面(GUI),里面有一些按钮,我可以把这些按钮拖到其他Windows应用程序里去。这样,其他应用程序就能根据我选择的按钮接收到特定的字符串。

请问,哪个Python的图形界面框架最简单,能支持这种拖放功能呢?

3 个回答

1

理论上,你可以使用任何有图形化拖放设计工具的库。这些工具通常会生成一种标记语言,然后这个库会解析这些标记,有时它们还会直接生成代码。直接生成代码是和编程语言有关的,而生成标记语言则不应该受限。无论如何,你总能找到用Python实现的方法。

实际上,有些库的可视化设计工具比其他的要好。我用过的WinForms设计器非常优雅且流畅,所以也许可以考虑IronPython?再比如PyGTK配合Glade,或者之前提到的PyQt?还有Jython使用NetBeans设计的Swing呢?

编辑:哎呀,我没有好好读问题。你想要的是一个有拖放功能的框架,也就是大多数框架都有这个功能。不过,有很多事情需要考虑,比如目标窗口和源窗口是否来自同一个进程?它们是否使用同一个框架编写?这些因素可能会影响到最终的实现。

1

任何一个用户界面库大概都有支持这个功能的方法。在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.

撰写回答