开发者

wxPython - Drag and Drop to StaticText in a dict

I have a problem using Drag and Drop in wxPython.

I have a ListCtrl from where I grab some text. This is the source, and it works well - I can drag the text I want to DnD and drop it e.g. into my editor and MS Word. So the sorce side is fine. For later use, I have stored the dragged text in self.chosenText

But I have some problem with the drop target.

My target is a StaticText (stored in a dictionary with key: [a list]) that is placed in a GridBagSizer.

The StaticText is defined like this and it works fine:

self.itemsInDict['a'][1] = wx.StaticText(self, -1, '\ndrag monomer here\n', style=wx.ALIGN_CENTER)

I have also defined for later use:

self.keyOfItemInDict = 'a'

And it is made a target like this - this also works, the mouse pointer indicates that this i开发者_如何学JAVAs a valid drop target:

target = DropTarget(self.itemsInDict['a'][1])
self.itemsInDict['a'][1].SetDropTarget(target)

Now, what I want is that the label of the StaticText is changed according to the text I drag from the list control. So I have created the class (please do not laugh, I really tried to understand this but failed...):

class DropTarget(wx.TextDropTarget):

    def __init__(self, object):
        wx.TextDropTarget.__init__(self)
        self.object = object
    # ----

    def OnDropText(self, x, y, text):

        self.object[self.keyOfItemInDict][1].SetLabel('\n'+self.chosenText+'\n')
    # ----

This understandably raises the error that DropTarget has no keyOfItemInDict. Using self.parent does not lead to a good result, as the StaticText seems to be the parent...

Could someone please point me in the right direction?


Stupid me - self.object alread is self.itemsInDict[self.keyOfItemInDict][1]. So

class DropTarget(wx.TextDropTarget):  

    def __init__(self, object):
        wx.TextDropTarget.__init__(self)
        self.object = object
    # ----


    def OnDropText(self, x, y, data):
        self.object.SetLabel('\n'+data+'\n')
    # ----

does what I need...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜