wx.ListBox drag and drop

7stud bbxx789_05ss at yahoo.com
Mon Aug 13 05:21:19 EDT 2007


On Aug 12, 11:06 pm, ianaré <ian... at gmail.com> wrote:
> Hey all,
>
> I see plenty of drag and drop examples but they are all for
> wx.ListCtrl. Anyone know of examples or tutorials for wx.ListBox?
> Specifically, I would like to be able to drag a selection from one
> ListBox to another.
>
> Thanks in advance,
>
> - ianaré

See if this helps:

-----
import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "List Boxes")
        panel = wx.Panel(self, -1)

        choices1 = ["red", "green", "blue"]
        choices2 = ["10", "20", "30"]

        self.lb1 = wx.ListBox(panel, -1, (10, 10),
                  (100, 200), choices1, wx.LB_SINGLE)
        self.lb1.Bind(wx.EVT_LISTBOX, self.on_select)


        lb2 = wx.ListBox(panel, -1, (120, 10),
                  (100, 200), choices2, wx.LB_SINGLE)
        target = MyTextTarget(lb2)  #MyTextTarget defined below
        lb2.SetDropTarget(target)

    def on_select(self, event):
        selection = self.lb1.GetSelection()

        text = self.lb1.GetStringSelection()
        text_obj = wx.TextDataObject(text)

        source = wx.DropSource(self.lb1)
        source.SetData(text_obj)

        drop_result = source.DoDragDrop(wx.Drag_DefaultMove)
        if drop_result == wx.DragMove:  #the selection was moved-not
copied
            self.lb1.Delete(selection)


class MyTextTarget(wx.TextDropTarget):
    def __init__(self, target_widget):
        wx.TextDropTarget.__init__(self)
        self.target_widget = target_widget

        self.text_obj = wx.TextDataObject()
        self.SetDataObject(self.text_obj)

    def OnData(self, x, y, default):  #called automatically on drop
        self.GetData()
        text = self.text_obj.GetText()

        end_of_list = self.target_widget.GetCount()
        self.target_widget.InsertItems([text], end_of_list)

        return default


app = wx.PySimpleApp(redirect=False)

win = MyFrame()
win.Show()

app.MainLoop()
---------

The EVT_LISTBOX appears to be slightly broken on a mac.  It doesn't
fire if you click on a selection that is already selected.




More information about the Python-list mailing list