2012-01-20 57 views
2

我想製作一個簡單的GUI,提供可拖放到其他Windows應用程序的按鈕,以便其他應用程序根據所選按鈕接收某個字符串。簡單的GUI Windows拖放

什麼是最容易的Python框架,允許這種拖放?

+1

你和誰一起工作過,你有什麼嘗試過?除了答案中提到的其他人之外,wxPython還全面支持拖放事件。 –

+0

我只做了一些TkInter。無論如何,我想我必須重新學習它。所以我需要一個非常簡單的地方,我有機會找到在線示例,展示如何使小部件可拖動並提供一些關於拖動的文本。我只需要一個提供可拖動文本的按鈕列表。 – Gerenuk

回答

1

任何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. 
+0

看起來很有希望。那是內部的wxPython或將它與其他Windows應用程序的工作?因爲我真正需要的是一些可拖動的標籤,它會在插入到Windows應用程序時插入字符串,所以我不擔心被刪除,我不需要複雜的對象,只需要一些字符串內容... – Gerenuk

+0

@ Gerenuk - CustomDataObject是我的一個更好的選擇,但您也可以使用'wx.TextDataObject'或'wx.PyTextDataObject'。您可以在這裏找到更多信息:http://wiki.wxpython.org/ListControls#Drag_and_Drop_with_lists –

1
+0

我看到:)但是,我找不到和示例做拖動(而不是放下)並提供定義的文本。你能幫我找一個參考嗎?我不知道這個框架:(編輯:嗯,它在下一頁:)希望它有幫助... – Gerenuk

+1

哇,PyQt似乎複雜:(我沒有看到一個簡單的方法來做拖動: ( – Gerenuk

+0

這是因爲它得到的那樣簡單。 –

1

理論上,你可以使用其中一個圖形拖放和拖放設計存在任何庫。這些工具通常會生成標記語言,這些標記語言會被庫解析,有時它們會直接生成代碼。後者是語言依賴的,而前者不應該。無論哪種方式,您都可以找到一種使用Python的方法。

實際上,一些圖書館擁有更好的可視化設計工具。當我使用WinForms設計器時,它非常優雅和無縫,所以也許IronPython? PyGTK,Glade或前面提到的PyQt怎麼樣?也許在NetBeans中使用Swing設計Jython?

編輯:哎呦,我沒有正確讀取的問題。你要找的是一個具有拖放功能的框架,即大多數框架。有很多事情需要考慮,例如,目標窗口和源窗口將來自同一個進程?他們會用相同的框架編寫嗎?這些東西可能是相關的或不相關的,取決於它是如何被寫入的。

+0

這是不同的過程。基本上,我想刪除文本字符串成任意的Windows應用程序。他們中的大多數支持。 – Gerenuk