如何解决在 Wxpython 中拖放图像
我在 wxpython 中创建了 3 个面板。一个在上面。和底部的其他 2 个面板垂直。 Panel2 由 ListControl 组成为 list1,Panel3 由 ListControl 组成为 list2。
我在 panel2 和 panel3 中将 wx.ImageList 与 wx.ListCtrl 结合使用。
我正在尝试将图像从 Panel2 拖到 Panel3。
用过的:
self.list1.Bind(wx.EVT_LIST_BEGIN_DRAG,self.OnDragInit,id=self.list1.GetId())
我在 wx.FileDropTarget
函数中使用了 wx.BitmapDataObject
来定义放置目标和 def OnDragInit
。
代码如下:
class MyTarget(wx.FileDropTarget):
def __init__(self,object):
wx.FileDropTarget.__init__(self)
self.object = object
def OnDropFiles(self,x,y,filenames):
print(filenames)
return(True)
def OnDragInit(self,event):
text = self.list1.GetBitmap(event.GetIndex())
too = wx.BitmapDataObject(text)
src = wx.DropSource(self.list2)
src.SetData(too)
src.DoDragDrop(True)
问题:放置目标不接受数据。
解决方法
我怀疑您没有设置 SetDropTarget
。
见这里,FileDrTr = MyFileDropTarget(self)
和 Drop_Place.SetDropTarget(FileDrTr)
import wx
import wx.lib.newevent
drop_event,EVT_DROP_EVENT = wx.lib.newevent.NewEvent()
class MyFileDropTarget(wx.FileDropTarget):
def __init__(self,obj):
wx.FileDropTarget.__init__(self)
self.obj = obj
def OnDropFiles(self,x,y,filename):
#filename is a list of 1 or more files
#here we are restricting it 1 file by only taking the first item of the list
TempTxt = filename[0]
evt = drop_event(data=TempTxt)
wx.PostEvent(self.obj,evt)
return True
class Example(wx.Frame):
def __init__(self,parent,title):
super(Example,self).__init__(parent,title=title)
self.InitUI()
self.Center()
def InitUI(self):
panel = wx.Panel(self)
FileDrTr = MyFileDropTarget(self)
font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
font.SetPointSize(9)
verBox = wx.BoxSizer(wx.VERTICAL)
horBoxOne = wx.BoxSizer(wx.HORIZONTAL)
TextLabel = wx.StaticText(panel,label = 'Drop file here')
TextLabel.SetFont(font)
horBoxOne.Add(TextLabel,flag=wx.RIGHT,border=10)
Drop_Place = wx.TextCtrl(panel)
Drop_Place.SetDropTarget(FileDrTr)
#Bind the drop event listener
self.Bind(EVT_DROP_EVENT,self.LabelTextUpdate)
horBoxOne.Add(Drop_Place,proportion=1)
verBox.Add(horBoxOne,flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP,border=10)
verBox.Add((-1,10))
horBoxTwo = wx.BoxSizer(wx.HORIZONTAL)
self.NewText = wx.StaticText(panel,label = 'Path will be')
horBoxTwo.Add(self.NewText,border=5)
verBox.Add(horBoxTwo,border=10)
panel.SetSizer(verBox)
def LabelTextUpdate(self,event):
txt = event.data
self.NewText.SetLabel(txt)
def main():
app = wx.App()
ex = Example(None,title = 'drop and see file path')
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
请注意,target
是 textctrl,将文件拖放到面板上将一事无成,而拖到 target
(名为 Drop_Place
的 textctrl)上将激活事件。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。