wxPython - 拖放到字典中的 StaticText
我在 wxPython 中使用拖放时遇到问题。
我有一个 ListCtrl,可以从中获取一些文本。这是源代码,而且效果很好 - 我可以将我想要的文本拖到 DnD 中,然后将其放入编辑器和 MS Word 中。所以源端没问题。 为了以后使用,我已将拖动的文本存储在 self.chosenText 中,
但是我在放置目标时遇到了一些问题。
我的目标是放置在 GridBagSizer 中的 StaticText(存储在字典中,键为:[a list])。
StaticText 是这样定义的,它工作得很好:
self.itemsInDict['a'][1] = wx.StaticText(self, -1, '\ndrag monomer here\n', style=wx.ALIGN_CENTER)
我还定义了供以后使用:
self.keyOfItemInDict = 'a'
并且它是这样的目标 - 这也有效,鼠标指针指示这是一个有效的放置目标:
target = DropTarget(self.itemsInDict['a'][1])
self.itemsInDict['a'][1].SetDropTarget(target)
现在,我想要的是StaticText 的标签根据我从列表控件中拖动的文本进行更改。所以我创建了这个类(请不要笑,我真的试图理解这一点但失败了......):
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')
# ----
这可以理解地引发了 DropTarget 没有 keyOfItemInDict 的错误。使用 self.parent 不会产生好的结果,因为 StaticText 似乎是父级......
有人可以指出我正确的方向吗?
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 is 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
愚蠢的我 -
self.object
已经是self.itemsInDict[self.keyOfItemInDict][1]
。我所需要的也是如此
...
Stupid me -
self.object
alread isself.itemsInDict[self.keyOfItemInDict][1]
.So
does what I need...